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:
- Open several files:
vim main.py utils.py config.py - Run
:lsto see buffer numbers - Type
:b 1to jump tomain.py - Type
:b 3to jump toconfig.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>.