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

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

  • :bprev moves to the previous buffer in the buffer list
  • :bp is 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 :ls or :buffers to list all open buffers with their numbers and status flags
  • Use :b N to 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 :bfirst to jump to the first buffer and :blast to 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

Next

How do I edit multiple lines at once using multiple cursors in Vim?