vimtricks.wiki Concise Vim tricks, one at a time.

How do I switch to a specific buffer by its number in Vim?

Answer

:b N

Explanation

How it works

Every buffer in Vim is assigned a unique number when it is opened. You can jump directly to any buffer by typing :b followed by the buffer number. For example, :b 3 switches to buffer number 3.

To see which number each buffer has, run :ls first:

  1 #    "main.py"     line 1
  2 %a   "utils.py"    line 5
  3      "config.py"   line 1
  7      "README.md"   line 1

The %a marker indicates the current active buffer, and # marks the alternate buffer. Notice that buffer numbers are not necessarily sequential -- if you close buffer 4, the number is not reused.

Example

A typical workflow looks like this:

  1. Open several files: vim main.py utils.py config.py
  2. Run :ls to see buffer numbers
  3. Type :b 1 to jump to main.py
  4. Type :b 3 to jump to config.py

You can also use :buffer N as the full command name. Additionally, you can combine this with a count before <C-^>: pressing 3<C-^> switches to buffer 3 without needing the command line.

This approach is faster than cycling through buffers with :bnext and :bprev when you know which buffer you want. For even faster switching, combine :ls with :b and tab completion of the filename using :b partial<Tab>.

Next

How do you yank a single word into a named register?