35

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?

22

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.

| improve this answer | |
  • 1
    Your answer says no, but it seems like that works great! – aharris88 Feb 5 '15 at 21:00
  • (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? – user72 Feb 5 '15 at 21:01
  • Yeah, it renumbers for me. – aharris88 Feb 5 '15 at 21:03
  • Yeah, I'm just dumb, I found my mistake. – user72 Feb 5 '15 at 21:04
  • 1
    :%bd will delete all buffers. – Yous Jan 4 '19 at 8:00
17

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

Hit :b se<Tab>

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 sctab then setab but here vim does an 'se' search and replaces it with the first (and only) match. If I had typed testtab 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.

| improve this answer | |
8

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

My tab line: My tab line

| improve this answer | |
  • amazing! I love it :) – Blasco Oct 2 '19 at 10:28
  • 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 and buffer_idx_mode=0 – caligari Oct 24 '19 at 11:28
  • Is there anyway to modify buffer index of airline? – fx-kirin yesterday
7

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.

| improve this answer | |
0

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:

enter image description here

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!

| improve this answer | |

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.