How do I switch to the previous buffer in Vim?
Answer
:bprev
Explanation
The :bprev (or :bp for short) command switches to the previous buffer in Vim's buffer list. Combined with :bnext, it lets you cycle through all open buffers sequentially.
How it works
:bprevmoves to the previous buffer in the buffer list:bpis the shortest unambiguous abbreviation- If you are on the first buffer, it wraps around to the last one
Example
You have three files open:
1 "main.go" line 42
2 "utils.go" line 1
3 %a "config.go" line 1
The % marker shows you are currently in config.go (buffer 3). Running :bprev switches to utils.go (buffer 2). Running :bprev again switches to main.go (buffer 1).
Tips
- Use
:bnext(or:bn) to switch to the next buffer - Use
:lsor:buffersto list all open buffers with their numbers and status flags - Use
:b Nto jump directly to buffer number N (e.g.,:b 2) - Use
<C-^>(Ctrl+6) to toggle between the current and the alternate (last used) buffer — this is usually the fastest way to flip between two files - Use
:bfirstto jump to the first buffer and:blastto jump to the last - Many users map these commands for quick access:
nnoremap [b :bprev<CR>
nnoremap ]b :bnext<CR>
- Use
:bd(buffer delete) to remove a buffer from the list entirely when you are done with it