Say, with 2 windows open in vim, cursor is window 2, how can I get the current buffer of the alternate window (window 1 in this case)?

1 Answer 1


Try this:

" current window buffer number
:echo winbufnr(winnr())

" 'previous' window buffer number
:echo winbufnr(winnr('#'))

" Name of the 'previous' window buffer number
:echo bufname(winbufnr(winnr('#')))

"Previous" here means the window you have switched from to the current with, for example, <C-w>w. So if you have 3+ windows opened then the last command will show you buffer name of the window you were in "previously" :)

To make it more "generic" you can try to play with winnr() function and friends.

  • Thx, with help the this answer, I solved the problem.
    – qeatzy
    Dec 21, 2018 at 10:59

Your Answer

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

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