Vim shortcut to open a file under cursor in an already opened window

editorsgvimkey mappingvivim

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

Best Answer

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

Explanation:

  • 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)
Related Question