0

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?

Rich
  • 31,891
  • 3
  • 72
  • 139

2 Answers2

1

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
endfunction
function! MySwitchBack()
  let b = bufnr("%")
  call altr#back()
  execute "bdelete " . b
endfunction

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

Rich
  • 31,891
  • 3
  • 72
  • 139
  • 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
  • The problem is that I can have half a dozen source files open, with all the headers that's a dozen files… It's getting hard to see what is where. – Sardathrion - against SE abuse Jan 24 '17 at 13:44
  • 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
  • Yes, I am coming to the same conclusion. Although, I would like to display tabs since that way I can see what pairs of files I have opened. – Sardathrion - against SE abuse Jan 24 '17 at 13:57
  • @Sardathrion If you do decide to try turning off permanent buffer display, you may also find the answers to this question and this one helpful. – Rich Jan 24 '17 at 13:57
  • I actually would rather learn how vim does it properly instead of hacking stuff that were not meant to be… ⍨ – Sardathrion - against SE abuse Jan 24 '17 at 14:10
  • 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
0

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.

Luc Hermitte
  • 17,351
  • 1
  • 33
  • 49