Opening files in the same folder as the current file, in vim

UnixVimVi

Unix Problem Overview


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

Unix Solutions


Solution 1 - Unix

:Ex short for :Explore does exactly what you asked for.

Solution 2 - Unix

I 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.

Solution 3 - Unix

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

Solution 4 - Unix

A 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.

Solution 5 - Unix

Try the following:

:e %:p:h/file2.js

Solution 6 - Unix

Add 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.

Solution 7 - Unix

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 </kbd>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

Solution 8 - Unix

You 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.

Solution 9 - Unix

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.

Solution 10 - Unix

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

Solution 11 - Unix

So 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.

Solution 12 - Unix

Another option is to do:

:e <Tab> 

This cycles through folders in your working directory.

Solution 13 - Unix

This works for me:

:vs | Ex #open vertical split window

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncloudheadView Question on Stackoverflow
Solution 1 - UnixsumekView Answer on Stackoverflow
Solution 2 - UnixhgmnzView Answer on Stackoverflow
Solution 3 - UnixgregfView Answer on Stackoverflow
Solution 4 - UnixCory EngebretsonView Answer on Stackoverflow
Solution 5 - Unixuser156676View Answer on Stackoverflow
Solution 6 - UnixrcsView Answer on Stackoverflow
Solution 7 - UnixMykola GolubyevView Answer on Stackoverflow
Solution 8 - UnixGene GoykhmanView Answer on Stackoverflow
Solution 9 - UnixJack M.View Answer on Stackoverflow
Solution 10 - UnixSteven MastandreaView Answer on Stackoverflow
Solution 11 - UnixNeg_EVView Answer on Stackoverflow
Solution 12 - UnixEricView Answer on Stackoverflow
Solution 13 - UnixRen GreenView Answer on Stackoverflow