4

I have two files open, each one is in a split.

From file one, how can I get the filename of the file in split two ?

In command mode, I can get the reference of the filename I'm currently editing by using %f, is there something similar that would return the filename of the file opened in my second split?

Update : Following Christian Brabandt's advice, I ended up doing this, from the first window :

:echo bufname(2)
1
  • If you want to use the file name somewhere else you can do something like let @[a-z0-9]=expand(@%) for each split where @[a-z0-9] is something like @s for split one and @w for split 2. Then you can use the registers as required. Could possibly do a function to set the next variable in a list to the filename on BufEnter or the like
    – Steve
    Oct 24, 2018 at 3:12

2 Answers 2

3

To get the buffer name in a different window, you will have to use a Vimscript solution.

You can refer to different splits using the window number or the winid. The window number is not stable and will change, if you modify the window layout, therefore the winid exists, which will never change (but was not available in older Vim versions). The window number starts with 1 from the top left window and is incremented for each window going from top left to bottom right.

Back to your question, in a Vim window with 2 split windows, you can use echo bufname(winbufnr(1)) to get the buffer name of the first split window and subsequently use echo bufname(winbufnr(2)) for the second window.

Making use of winids is left as an excercise to the reader ;)

2
  • I'm not sure winnr() takes numbers as an argument, echo bufname(winnr(1)) returns E15: Invalid expression: 1. But simply doing echo bufname(2) from the first window did the trick ! Thanks.
    – mike23
    Oct 30, 2018 at 19:20
  • yeah, I think I wanted to use bufname(winbufnr(1)) Oct 31, 2018 at 18:03
0

focus the split <C-w>h/j/k/l then hit <C-g>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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