5

I usually have two windows open, one in which I'm working and one in which I'm viewing some information (compiler messages, relevant library headers, etc. etc.). Now, it's easy to switch the buffer on which I'm working with :b, which offers nice tab-completion – however, that's not actually necessary very often since I usually spend substantial time working on a single file. What's needed more often is switching the buffer in the other window, however to do that I now use the pretty awkward ^w p :b foo ^w p combination.

Is it possible to choose that buffer without actually switching to the other window and then back again?

1
  • Incidentally, the Emacs shortcut for this action is ^x 4 b. Commented Jul 31, 2017 at 13:22

2 Answers 2

5

Maybe you could use something like this:

function! ChangeWindow(buf)
    execute "wincmd p"
    execute "e " . a:buf
    execute "wincmd p"
endfunction

command! -complete=buffer -nargs=1 CW call ChangeWindow("<args>")

You create a function ChangeWindow() which takes as argument a buffer name and execute the commands you usually do manually (ctrl+p, :e newBuf, ctrl+p)

You also create a command with -complete=buffer so it will provide the same autocompletion as :b and it will call the function with the argument to gave.

This way you only have to do :CW foo to put the buffer :foo in the previous window.

2
  • That works very nicely, though I would prefer something that'll work out of the box without my custom .vimrm. Commented Jul 31, 2017 at 11:57
  • @leftaroundabout I'm not aware of a build-in feature which would allow you to do that but maybe someone will prove me wrong :-)
    – statox
    Commented Jul 31, 2017 at 12:40
4

If you have a limited amount of buffers to cycle through instead of needing to open a specific buffer or file you could use :bnext or some variant of that. If you combine that with a :map-variant and some window switching you could get this:

nnoremap <F8> <C-W>w:bnext<CR><C-W>W
nnoremap <S-F8> <C-W>w:bprevious<CR><C-W>W

Here F8 and Shift-F8 will be mapped to cycled forward or backwards between the buffers in the next window.

Add that to your .vimrc and you are good to go. If you do not want to do that just add it as a regular command.

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.