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

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)

This worked for me:

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

nmap ,f :call OpenFileInPrevWindow()<CR>