How do I switch to the next buffer in Vim?
Answer
:bnext
Explanation
The :bnext (or :bn for short) command switches to the next buffer in Vim's buffer list. Buffers are Vim's way of keeping multiple files open simultaneously, and :bnext cycles through them in order.
How it works
:bnextmoves to the next buffer in the buffer list:bnis the shortest unambiguous abbreviation- If you are on the last buffer, it wraps around to the first one
Example
You have three files open:
1 %a "main.go" line 42
2 "utils.go" line 1
3 "config.go" line 1
The % marker shows you are currently in main.go. Running :bnext switches to utils.go. Running :bnext again switches to config.go.
Tips
- Use
:bprev(or:bp) to switch to the previous buffer - Use
:lsor:buffersto list all open buffers with their numbers - Use
:b 3or:b3to jump directly to buffer number 3 - Use
:b filenameto switch to a buffer by partial file name (with tab completion) - Use
<C-^>(Ctrl+6) to toggle between the current and the alternate (last used) buffer — this is often faster than:bnextwhen flipping between two files - Use
:bfirstand:blastto jump to the first or last buffer - Many users map
:bnextand:bprevto more convenient keys:
nnoremap ]b :bnext<CR>
nnoremap [b :bprev<CR>