How do I list all open buffers in Vim?
Answer
:ls
Explanation
The :ls command displays a list of all open buffers in Vim, showing their buffer number, status indicators, file name, and the line the cursor was last on. This is essential for managing multiple files within a single Vim session.
How it works
:ls(or:buffersor:files) shows all loaded buffers- Each buffer has a unique number that persists for the lifetime of the Vim session
- Status indicators tell you the state of each buffer
Example output
1 %a "main.go" line 42
2 #h "utils.go" line 1
3 "config.go" line 15
Status indicators explained
%— the buffer in the current window#— the alternate buffer (switch to it with<C-^>)a— active, loaded and displayed in a windowh— hidden, loaded but not displayed in any window+— modified, the buffer has unsaved changes-— buffer hasmodifiableturned off=— buffer is readonly
Tips
- Use
:b Nto switch to buffer number N (e.g.,:b 3to jump to buffer 3) - Use
:b filenameto switch to a buffer by partial file name with tab completion - Use
<C-^>(Ctrl+6) to toggle between the current buffer (%) and the alternate buffer (#) - Use
:bnext(:bn) and:bprev(:bp) to cycle through buffers in order - Use
:bdelete N(:bd N) to close a specific buffer by number - Use
:ls!to also show unlisted buffers (like help files and scratch buffers) - Use
:bufdoto run a command on every buffer — for example:bufdo %s/old/new/greplaces text across all open buffers