Last Updated: February 25, 2016
·
601
· NIA

Open file from same dir as edited file in vim

It is easy to do so with :e file.name or :sp file.name when file.name is in the working dir of Vim.

But often you open some file in a very far dir with vim very/far/dir/some.file and then want to open its sibling it the same dir. You can't do it with :sp file.name, you need :sp very/far/dir/file.name or :sp, :E and select file manually.

Bo-o-o-ring.

There is a solution! This vim tip suggests three different ways of doing it, here is my favorite:

cabbr <expr> %% expand('%:p:h')

Add this to your .vimrc, restart vim or re-source .vimrc, and now you can do, for example

:sp %%/file.name

and when you enter /, %% will expand to the full path of opened file.

Bonus: Probably you don't actually want path to be full because it may be too long? If relative to the working dir is good for you, just remove :p part from expand:

cabbr <expr> %% expand('%:h')

Currently I prefer this modification, as you can see from my vimrc