After I've been working on a project for a while, I start to see large gaps between consecutive buffer numbers. This is because the buffers in between them were wiped out for various reasons. Unfortunately, this can make it awkward to jump to a particular buffer, or select a range of buffers, after typing :ls
. Is there some way to renumber all of the open buffers, starting from one, without opening all of the files again?
6 Answers
No (not without deleting buffers).
Vim does not support manually buffer number assignment or re-ordering of buffers once you open them. It's philosophy is that each buffer gets an identifier that is fixed for the lifetime of that buffer (in the help for :ls
, it notes that "each buffer has a unique number. That number will not change...").
You could use the argument list, though. Put all open buffers into the argument list, delete all outstanding buffers, then open everything in the argument list. The following commands will accomplish that:
:argdel *
(delete the existing argument list):bufdo argadd %
(for each buffer, add the buffer's path to the list):1,1000bd
(delete buffers 1 to 1000; probably there's a better way to do this):argdo e
(for each argument, edit that argument)
This will leave you with an extra empty buffer that vim opens when you delete all the previous buffers, but it's a reasonably approximation of the functionality you want. You can just :bd
that extra buffer.
-
1
-
(I meant "no" in the sense that you can't do it without re-opening files). Does it actually renumber from 1 for you though? I thought it did when I first tried it, but now I'm seeing it not renumber for me?– user72Feb 5, 2015 at 21:01
-
-
Yeah, I'm just dumb, I found my mistake.– user72Feb 5, 2015 at 21:04
-
3
"the buffer numbers get crazy" Tell me about it! By the end of the day I'm easy over 100 buffers. But luckily, as you can see in this animation, you have tab completion for buffer names.
So, even though you can't renumber the buffers, you can still jump around easily.
I don't know if it's clear from the animation but, the "tab completion" is unlike command line tab completion. It's more like a "ambiguity resolving tab replacer". Meaning that at the command line I would have had to type sc
tab then se
tab but here vim does an 'se' search and replaces it with the first (and only) match. If I had typed test
tab it would have matched 'generate_test_data.py' and then tab again would have matched 'scripts/setup_test_data.sh' and then tab again would have cycled back around, etc.
The point being that you can get in the habit of typing merely :b <minimum amount of unambiguous characters>
tab to jump to the document you want. I think this is even better than remembering "My buffers are 1:foo 2:bar 3:etc" which is how I used to do it when I was a newbie.
According to the documentation, the buffer numbers never change
Each buffer has a unique number. That number will not change,
so you can always go to a specific buffer with ":buffer N" or
"N CTRL-^", where N is the buffer number.
The only way I can think to renumber the buffers is to restart vim.
You can use vim-airline. This plugin has a behavior that maybe fix you problem:
See line 470-479 in airline.txt
This will not change the buffer numbers, but you can select a buffer from left to right by the row number in the tabline.
This is my setting for airline in .vimrc
:
"set airline
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#tab_nr_type = 1 " tab number
let g:airline#extensions#tabline#show_tab_nr = 1
let g:airline#extensions#tabline#formatter = 'default'
let g:airline#extensions#tabline#buffer_nr_show = 1
let g:airline#extensions#tabline#fnametruncate = 16
let g:airline#extensions#tabline#fnamecollapse = 2
let mapleader=","
let g:airline#extensions#tabline#buffer_idx_mode = 1
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
nmap <leader>3 <Plug>AirlineSelectTab3
nmap <leader>4 <Plug>AirlineSelectTab4
nmap <leader>5 <Plug>AirlineSelectTab5
nmap <leader>6 <Plug>AirlineSelectTab6
nmap <leader>7 <Plug>AirlineSelectTab7
nmap <leader>8 <Plug>AirlineSelectTab8
nmap <leader>9 <Plug>AirlineSelectTab9
-
-
Not the answer to the question but it patches the problem with the crazy buffer numbers. Just a recommendation to show buffer_numbers only:
switch_buffers_and_tabs=1
andbuffer_idx_mode=0
– caligariOct 24, 2019 at 11:28 -
1
One way to "garbage collect" the unused buffer numbers that exist due to buffers having been deleted with :bd
, one way is to save the session, then exit Vim and restart the session.
:mksession! mysession
:q
Then re-invoke Vim:
vim -S mysession
The session file creates the buffers using a sequence of badd
commands which will allocate new, consecutive numbers; the session doesn't restore the original buffer numbers.
Old question, but since this comes up at the top in google search, I'll leave this here for future searches. As Bruno suggested, a fuzzy finder that supports buffer lists is the way to go. I personally use and recommend fzf-vim. It provides the command :FZFBuffers
, that looks something like this:
Buffer numbers are displayed to the left in case you need them, but with a fuzzy finder, you might never need to use them again. Just remember part of the file name, and it's usually 2-5 keystrokes away. Personally, I've mapped this command to a keystroke, and switching between buffers is almost instant for me!