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

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 :buffers or :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-^>)
  • aactive, loaded and displayed in a window
  • hhidden, loaded but not displayed in any window
  • +modified, the buffer has unsaved changes
  • - — buffer has modifiable turned off
  • = — buffer is readonly

Tips

  • Use :b N to switch to buffer number N (e.g., :b 3 to jump to buffer 3)
  • Use :b filename to 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 :bufdo to run a command on every buffer — for example :bufdo %s/old/new/g replaces text across all open buffers

Next

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