In vim, when I have a buffer open, I often need to load another file in the same directory that file is in, but since I don't usually cd into it, the pwd is a parent folder, so I have to retype the path every time. Is there a shortcut for this? or a way to change the pwd to the directory the file is in?
example:
cd /src vi lib/foo/file.js lib/foo has two files: file.js and file2.js
in vi:
:e file2.js # doesn't work 13 Answers
:Ex short for :Explore does exactly what you asked for.
5I have the following three lines on my .vimrc:
map ,e :e <C-R>=expand("%:p:h") . "/" <CR> map ,t :tabe <C-R>=expand("%:p:h") . "/" <CR> map ,s :split <C-R>=expand("%:p:h") . "/" <CR> Now ,e <some-file> opens on this buffer. ,t and ,s do the same but on a new tab/split window.
Newer versions of vim have a autochdir command built in, if not you can fall back to a BufEnter like setup.
" set vim to chdir for each file if exists('+autochdir') set autochdir else autocmd BufEnter * silent! lcd %:p:h:gs/ /\\ / endif 1A simple idea is to use the autochdir setting. Try this in your .vimrc:
set autochdir This will change the working directory of vim to the directory of the file you opened.
Note: I'm not sure what version added this feature. I updated from vim 6 to 7.2 to get this to work.
3Try the following:
:e %:p:h/file2.js 3Add this line to your vimrc configuration
autocmd BufEnter * cd %:p:h % current file name
:p expand to full path
:h head (last path component removed)
See :help expand for further information.
The solution with autochdir has its own pitfalls. For example you have to stay at the dir you are because Tags are defined with relative path or you need Makefile or pom.xml in the current dir.
You can use
:e <C-R>% and then modify path by deleting name of the current file and enter new one. Or. Use the map
nnoremap <leader>e :edit <C-R>=fnamemodify(@%, ':p:h')<CR>/ And then by pressing \e or ,e (depends on leader settings) you will receive command that open directory in which your current file is and you can complete this command manually and can use ctrlD to show all files from the current file directory.
Or. You can try 0scan plugin.
Keys 0f
1You probably know that once you're in vim, you can use
:cd lib/foo to change into lib/foo.
On the other hand, to change into the directory of the file you're current editing, try
:cd %:p:h You can always use
:pwd to check your current working directory.
And of course, if you forgot what file you're editing, just hit ctrl-g.
2So vim has a :find command which will use the variable 'path'. If you have consistent directory structures for your code such as all source in under a directory "src" then you can just add that to the 'path' variable. Then when you do :find foo.js it will search both your current location then the folders listed in your path variable. It basically emulates the way bash or some other shell uses the PATH variable to find the appropriate binary to run.
On the "shortcut" front, have you looked into the :Sex command? This opens up the file browser in a split so that you can open other files easily. :Sex default to the directory of the current buffer. Pretty nifty feature, if I say so myself.
I bind it to ;o for easy access:
map ;o :Sex <CR> You can also use :Ex if you want to keep it in the current buffer instead of creating a split.
I put this in my .vimrc file, which will change the current directory to the file in the buffer. This sets the current directory for the buffer, and updates when you switch buffers.
autocmd BufEnter * lcd %:p:h 0Another option is to do:
:e <Tab> This cycles through folders in your working directory.
1This works for me:
:vs | Ex #open vertical split window