Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to save a command in vim? I want to be able to go straight to a directory after login, such as:

cd directory/subdirectory/files

Thank you for any ideas.

share|improve this question
    
I think this question should be rephrased, but I'm hesitant to just edit it myself because I might misinterpret the ambiguity. Perhaps you mean "Automatically execute a command on startup" instead of "Save a command"? And do you mean when you log in and get a shell, or within Vim when you start it? – Heptite Dec 30 '12 at 1:27
up vote 4 down vote accepted

If you want to have your shell cd to a particular directory after login, then put this in your ~/.bashrc (assuming you're using bash):

if [ shopt -q login_shell ]
then
    cd directory/subdirectory/files
fi

If you want to have vim cd to a particular directory each time you open it, then put this in your ~/.vimrc:

cd directory/subdirectory/files

Note that the cd in your ~/.vimrc is not the shell's cd but is a Vim command. See

:help :cd
share|improve this answer
    
Thank you for your answer. The only problem is that i have different directories on separate parts of the filesystem that i would like quick access to. Any ideas that might help me? – Nick B Dec 29 '12 at 21:15
    
There are a lot of tools that can help with that. However, I'm not very familiar with many of them. From the command line, I like using wcd (wcd.sourceforge.net). For within Vim, I've heard good things about FuzzyFinder (vim.sourceforge.net/scripts/script.php?script_id=1984) and ctrlp (vim.sourceforge.net/scripts/script.php?script_id=3736). You can also use bash aliases to create commands for jumping to frequently-used directories, e.g., alias cda='cd /long/path/to/dir'. – garyjohn Dec 29 '12 at 22:00

In your ~/.vimrc, you can define custom commands (like :CdA) that quickly switch to directories; e.g.:

:command! CdA cd directory/subdirectory/files
:command! CdB cd directory/other_directory/files
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .