I am currently using vim-altr to switch between head and source file in c++. I use

nmap <F2> <Plug>(altr-forward)
nmap <F3> <Plug>(altr-back)

to move between buffer. However, this opens two buffers. I would like it to be in one (and only one) buffer.

If this cannot be done by vim-altr, is there another way?


I think you're misunderstanding the terminology slightly, here. Essentially, a Vim buffer is a file, so loading two files into a single buffer isn't a concept that really makes sense.

It seems from your comments that the real issue is that you don't want to have both files in the buffer list (as displayed by vim-airline) at the same time.

I don't recommend that you do this (I think it's better to just get used to the way that Vim works), but if you really want to, you can achieve this by altering the mappings you use to call the altr plugin:

function! MySwitchForward()
  let b = bufnr("%")
  call altr#forward()
  execute "bdelete " . b
function! MySwitchBack()
  let b = bufnr("%")
  call altr#back()
  execute "bdelete " . b

nmap <F2> :call MySwitchForward()<CR>
nmap <F3> :call MySwitchBack()<CR>

These mappings use altr to switch buffers, and then delete the buffer you just switched away from.

N.B. In my testing with the a.vim plugin, this approach didn't work well, because of the method a.vim uses for opening the buffer. I'm pretty sure it will work with the altr plugin you are using, but if not, you will need to replace "bdelete" with "bwipeout" (although make sure you are aware of the consequences before doing so).

| improve this answer | |
  • Having been learning a lot more about buffers vs tabs, your assertion that I was confused by the two is indeed correct. – Sardathrion - against SE abuse Jan 24 '17 at 13:43
  • 3
    @Sardathrion My personal feeling is that you'd be better off by leaving the buffers alone, and instead stop displaying them all in vim-airline. If that doesn't work for you, though, then the mappings in my answer are the only real solution. – Rich Jan 24 '17 at 13:56
  • 1
    I often open a dozen pair of windows for header file + implementation file. I display them side by side (thanks to a.vim :AV command). Then I navigate with a tag explorer plugin (there are plenty). In all cases, I never invest any attention to the buffers that are opened/loaded. – Luc Hermitte Jan 24 '17 at 14:13
  • 2
    @Sardathrion As Luc Hermitte suggests, the standard vimmy way is not to care which buffers are loaded. The question to ask yourself is "why do I need the list displayed onscreen at all times?". If you don't have a good answer, then try turning it off! Here's another good resource. If you do have a good answer, maybe there is another way of achieving the same goal, using the functionality Vim already provides. – Rich Jan 24 '17 at 14:37
  • 1
    If you do really need all the buffers listed onscreen, you might prefer minibufexplorer to airline's display: it uses multiple lines when the list is long. – Rich Jan 24 '17 at 14:39

The venerable alternate plugin (a.vim), has a :A command that seems to be exactly what you're looking for -- changing the buffer currently displayed in the current window, however you'll see several buffers with :ls.

Its API is a little bit cumbersome though and it doesn't provide a neat way to have different settings for different projects -- I guess this is why several other similar plugins have emerged since then.

| improve this answer | |
  • Hum, I get the same behaviour. Although, I am using vim-airline buffer display so I guess it shows all the buffers which makes sense… – Sardathrion - against SE abuse Jan 24 '17 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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