Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: How to open another file from same location as an already open file

Tags:

vim

Let's say I am in my home directory. I open a file that is present in some deep nested directory structure:

vim /some/really/long/path/file.txt

Now, within vim, I want to start a vertical split with another file from that same long location (but I don't want to change the current directory to that path, I still want to be in the home directory).

A layman (like me) will do something like:

:vsp /some/really/long/path/file2.txt

But I want to find out from all you VIM geniuses out there, is there an easier way to do this?

On the same note, if I had multiple files already open residing in different paths, can VIM automatically assign some sort of internal variables to all the locations? And then when I want to start a new VSP or SP with one of the files from one of those locations, I simply use those internal variables?

like image 784
shikhanshu Avatar asked Feb 09 '16 21:02

shikhanshu


2 Answers

Try this:

:vs %:p:h/otherfile

as %:p:h gives you the path of the current file. See :help %:p for more info.

like image 108
ntki Avatar answered Oct 31 '22 03:10

ntki


Edit another file in the same directory:

:vs %<Tab><C-w><C-w><C-w>file2<Tab>

With a mapping:

nnoremap <key> :vs <C-R>=expand('%:p:h')<CR>
like image 22
romainl Avatar answered Oct 31 '22 03:10

romainl