Search code examples
vimeditorbuffer

Jump to vim buffer by number?


I have some "buffer bar" at the top, showing my buffers like tabs in a browser.

In a browser I can ctrl-1, ctrl-2 etc. to jump to tabs.

What can I do in vim to jump to the buffer I want really quickly?


Solution

  • I have some "buffer bar" at the top, showing my buffers like tabs in a browser.

    What can I do in vim to jump to the buffer I want really quickly?

    Since there is no built-in "buffer bar" in Vim you must have installed a third-party plugin which, hopefully, comes with some documentation. Did you read it? Since you didn't tell us what plugin you use we can only answer with built-in commands.

    The built-in command for navigating to buffer 6 is :b6. If you don't know a buffer's number you can:

    • cycle through the buffer list with :bn[ext] and :bp[revious],
    • do :ls (or :buffers, or :files, the three are synonymous) followed by :b6,
    • do :b foo<Tab>.

    The last method being the most efficient and scalable.

    Oh I just checked with :buffers, and the buffers I have open have the numbers 5, 6, 9, etc. all random. How do I choose from these?

    No, buffer numbers are not "random" at all. When a buffer is added to the buffer list it is assigned a fixed number that's equal to the number of the last buffer in the list plus 1. The holes in your list are either due to:

    • some buffers being hidden, you can see them with :ls!,
    • or some buffers being wiped out from the list.