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