This tip allows you to open files easily / quickly in the same directory as the file you're currently working on in Vim. By default, Vim opens files from the current working directory when you hit ':e' which is annoying if you want to quickly open a file in the same directory as the file you're working on without having to drill down through lots of subdirectories. Using this key mapping tip though you can open files from the working directory of the file you're currently working on by hitting ',e'.
The tip includes an example, it's really worth trying if you use Vim as your main editor - personally I'd find it hard working without it in Vim now.
First of all it's worth noting that there is a Vim setting that allows you to have the working directory follow the file you're currently working on:
CODE:
# Add this to ~/.vimrc to make it permanent:
:set autochdir
However the following is still relevant because it's not always the case you want this behaviour and the keystroke ',e' is as quick as ':e' to type anyway to get this equivalent functionality to work.
Here goes with the ',e' mapping:
Add the following key mapping to ~/.vimrc:
CODE:
map ,e :e <C-R>=expand("%:p:h") . "/" <CR>
Save the ~/.vimrc file.
Read in the new mapping by sourcing the ~/.vimrc:
CODE:
:source ~/.vimrc
or just exit and restart vim.
Now the ',e' mapping should work. To test it:
Open a file NOT in the current working directory:
CODE:
# (this is in Vim):
# /root is my current working directory
:!echo $PWD
/root
# open up a file in another directory:
:e /var/log/messages
# now try the new key mapping - when you hit the key stroke ',e' you get:
:e /var/log/
# hitting <TAB> allows autocompletion on all files in /var/log as well :)
I should give credit where it's due though, I found this tip on the
Vim Tips site - always worth searching through if you ever need to get something done in Vim. Easier still,
download all of the Vim Tips submitted on the site, then just search for the keywords that you're looking for a tip on (searching in it for ',e' I just found out the tip above was
tip #2 (of 1327!!!)). Credit goes to those guys in tip #2 for the above. :)
Mmm... just reading through those comments I'm not sure if I'll bother posting this article, seems there's an even easier way to have the working dir follow the current file using ':set autochdir'... ack and I'm so used to using ,e now... then again though there are some times when you don't always want that autochangedir functionality. Ho hum I'll stick with ',e' I think.