Vim Recipes Navigation Navigating Buffers

Navigating Buffers

Problem

You have multiple files open and want to navigate between them.

Solution

Buffers allow you to have a collection of files open with only one displayed at a time. They are particularly useful for processing a set of files sequentially, whereby you operate on one file, then switch to the buffer containing the next. If you'd rather have the open files listed along the top of the screen, consider using tabs instead.

Command Result
:buffers View the list of buffers along with their numbers.
:ls
:files
:buffer N Open buffer N.
:bn[ext] Go to the next buffer. (Menmonic: buffer next).
:bp[revious] Go to the previous buffer. (Mnemonic: buffer previous).
:bf[irst] Go to the first buffer.
:bl[ast] Go to the last buffer.
:ba[ll] Open all the buffers in the buffer list. (Mnemonic: buffer all, or have a ball, go crazy and open them all at once).

Discussion

To quickly navigate between buffers its common to map :bnext and/or :bprev to a key. For example map <F6> :bn<CR> lets you hit <F6> to cycle through the open buffers.

You can also switch buffers very quickly by using :buffer (and its shortcut :b). It supports (partial) file matching thus if you have first.txt and second.txt loaded you can use :b fir (or even just :b f) to switch to first.txt.