22

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

psibar
  • 461
  • 1
  • 3
  • 10

5 Answers5

14

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)
Damien
  • 748
  • 8
  • 14
psibar
  • 461
  • 1
  • 3
  • 10
  • I tweaked your command a bit, so that it does not use the next split, but the previously used one. I also don't jump back but stay in that split: `nnoremap :let mycurf=expand("")p:execute("e ".mycurf)` – Jounathaen Sep 11 '20 at 13:20
  • And if you use `` instead of `` it opens the file on the correct linenumber (given the linnumber is specified after the filename like _/my/file:123_). Might break on other cases. – Jounathaen Sep 11 '20 at 13:50
  • Er, the `` trick dosen't work, at least for me — if I run `edit %:50` for instance, it doesn't jump to line 50, it just edits a file named `thefileiwasin.js:50` instead. I'm in Neovim 0.5. Maybe it's a Vim-8-only thing? /= – ELLIOTTCABLE Aug 24 '21 at 00:59
4

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.

waldauf
  • 365
  • 1
  • 3
  • 11
2

This worked for me:

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

nmap ,f :call OpenFileInPrevWindow()<CR>
sirex
  • 121
  • 2
1

I got annoyed enough about the above answers from 2013 not quit working reliably in the situations I cared about, and wrote slightly-more-robust functions to handle them. For gF specifically (i.e. including the "goto linenumber" functionality), as an example:

function! gfriend#goto_cWORD(winmotion)
  let cword = expand("<cWORD>")
  let st = match(cword, '\v\f+:\d+')
  let end = matchend(cword, '\v\f+:\d+')

  if end !=# -1
    let cword = cword[st:end - 1]
  endif

  let bits = split(cword, ':')

  let starting_window = nvim_win_get_number(0)

  " If there's no previous window, create a new one
  wincmd p
  if nvim_win_get_number(0) ==# starting_window
    execute(a:winmotion." ".bits[0])
  else
    execute("e ".bits[0])
  endif

  if bits[1]
    execute(bits[1])
  endif
endfunction

Usage is something like:

nmap <silent> <Leader>gF \
   :<C-u>call gfriend#goto_cWORD(winwidth(0) >=# 180 ? 'vsp' : 'sp')<CR>

I've posted the (tiny) thing to GitHub, from whence you can install it as a plugin if you like:

Plug 'ELLIOTTCABLE/vim-gfriend'
ELLIOTTCABLE
  • 119
  • 5
1

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
Ingo Karkat
  • 11,664
  • 1
  • 34
  • 48
  • 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 03 '13 at 14:13