In vim you can open a file under the cursor by using the gf command.

One can also easily open that file in a new split window by hitting <c-w> f. This is a really nice and time saving feature.

However, I can't figure out, how to open the file in an already opened split window (without creating a new one).

  • 1
    You may want to have a look at the preview feature (see :h preview). – Stéphane Chazelas May 3 '13 at 15:01
  • Right, I use preview as well. However I didnt want to open the file in the preview window but explicitely in an other one. – psibar May 3 '13 at 15:04

I got all the pieces together to do the trick. The best way is to create a custom mapping for all the commands:

 map <F8> :let mycurf=expand("<cfile>")<cr><c-w> w :execute("e ".mycurf)<cr><c-w>p


  • map <F8> maps on "F8" the commands that follow
  • let mycurf=expand("<cfile>") gets the filename under the cursor and saves it in mycurf
  • <c-w>w changes the focus to the next open split window
  • execute("e ".mycurf) opens the file saved in mycurf
  • finally <c-w>p changes the focus to the previous window (where we actually came from)

That can't be done easily. A [count] before <C-w>f specifies which file match on 'path' is opened, it does not select an existing window. Only for the <C-w>w command, [count] means "go to existing window number".

To get that functionality, you need to write a custom mapping which either

  • grabs the file, goes to the [count] window and emulates the gf command, or
  • clones the current buffer to the [count] window, and executes gf there
  • Yes the only way to do it is to write a custom mapping. I was just having some trouble with grabbing the file under the cursor. But I think I have figured it out now – psibar May 3 '13 at 14:13

I searched for the same VIm's function and found out this solution which works like charm:

map <F8> :vertical wincmd f<CR>

Source page.


This worked for me:

function! OpenFileInPrevWindow()
    let cfile = expand("<cfile>")
    wincmd p
    execute "edit " . cfile

nmap ,f :call OpenFileInPrevWindow()<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.