Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Buffers, Windows and Tabs

Buffers: Working With Many Files

Understand Vim buffers: the difference between a file, a buffer and a window, how to list, switch and close buffers, the hidden option, and the alternate file.

Beginners often think that Vim opens a file in a window. It does not. Vim reads the file into memory as a buffer, and a window is only a viewport that shows a buffer. Once that picture is clear, the rest of this chapter (splits, tab pages, the quickfix list) falls into place.

In this lesson you learn what a buffer is, how to list and switch between buffers, why Vim sometimes refuses to leave a modified one, and which commands close a buffer for good.

A note on the runnable examples in this chapter: Vim and Neovim cannot run inside this site. The editors in the lessons are small JavaScript stand-ins, labelled as such, that model the same rules (a buffer list, a window layout, a quickfix list) so you can experiment with the ideas. Every Vim command shown in the text is described from how the real editor behaves.

Buffer, window, tab page

Three words, three jobs.

  • A buffer is the in-memory text of a file (or of nothing: a scratch buffer). It has a number, a name and a modified flag.
  • A window is a viewport onto one buffer. The same buffer can appear in several windows, and each window has its own cursor and scroll position.
  • A tab page is a collection of windows, a layout. It is not a file tab like in other editors.

Three layers

Buffer The text in memory
Window A view of one buffer
Tab page A layout of windows

Buffers hold text, windows show it, tab pages arrange windows.

1 / 2

Listing buffers

Start Vim with several files (vim a.txt b.txt c.txt) or open them with :e. Every file you touch becomes a buffer. Use :ls (also :buffers or :files) to list them:

TEXT
  1 %a   "a.txt"                        line 1
  2 #h   "b.txt"                        line 1
  3      "c.txt"                        line 0

The flags in the second column matter:

Flag Meaning
% The buffer shown in the current window
# The alternate buffer, the one you were in before
a Active: loaded and visible in a window
h Hidden: loaded but not visible
+ Modified: has unsaved changes
- Not modifiable

A buffer with no a or h is listed but not loaded; Vim has only remembered the name. :ls! also shows unlisted buffers such as help pages.

Switching

Command What it does
:b 2 Go to buffer number 2
:b name Go to the buffer whose name contains name (press Tab to complete)
:bn / :bp Next / previous buffer in the list
:bf / :bl First / last buffer
Ctrl-^ or :b# Jump to the alternate buffer
:e # Same, spelled with the edit command

Ctrl-^ is the single most useful of these. When you flip between a source file and its test, one keystroke does it. Typing a unique part of a name (:b conf) is usually faster than counting numbers.

JS Stand-in: a buffer list with an alternate file

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Why Vim refuses to leave a modified buffer

By default, if the current buffer has unsaved changes and you try to switch away in the same window, Vim shows E37: No write since last change (add ! to override). This is a safety rule: a buffer that is not visible in any window and not saved could be forgotten.

You have three ways out:

  1. Save first: :w, then switch (or :wn to write and go to the next buffer).
  2. Force it with !: :b! 2 hides the modified buffer and switches. Your edits are not lost; the buffer is simply hidden.
  3. Turn on the hidden option once and for all: :set hidden in your vimrc. Vim then lets any modified buffer be hidden without complaint.

Modern Neovim already sets hidden by default. With it, :ls shows h+ for a hidden buffer with unsaved changes. The risk is quitting: :q will warn if hidden buffers are modified, and :wa writes them all, :wqa writes everything and quits, :qa! quits and discards everything.

Closing buffers

There are three levels of "close", and they differ.

  • :q closes the window. If it was the last window, Vim exits.
  • :bd (:bdelete) removes the buffer from the list. Windows showing it are closed. The text is unloaded.
  • :bw (:bwipeout) also forgets the buffer's marks, options and number completely. It is for scratch buffers and plugin authors.
  • :bd 3, :3,5bd and :bd a.txt b.txt close several at once.

If you want to close a buffer but keep the window layout, you need a small mapping or plugin, because :bd closes the windows too. The later configuration chapter shows how.

Useful buffer commands

  • :badd path adds a file to the list without loading it.
  • :bufdo cmd runs an Ex command in every listed buffer, for example :bufdo %s/old/new/ge | update. The e flag stops errors when a buffer has no match, and update writes only if changed.
  • :sb 2 opens buffer 2 in a new split (a window, see the windows lesson).
  • :ball opens one window per buffer; :unhide does the same for hidden ones.
  • :e! throws away the changes in the current buffer and rereads the file.
  • :enew opens a new empty buffer; :file name sets its name.
JS Stand-in: which buffers block quitting?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does :q close?

    A window is only a view. :q closes the window; the buffer remains until you use :bd or :bw.
  2. 2. Which key jumps back to the alternate (previously edited) buffer?

    Ctrl-^ switches to the alternate file, shown with # in :ls. It is equivalent to :b#.
  3. 3. Why does Vim show E37 when you try to change buffers?

    Without 'hidden', a modified buffer cannot leave its window. Write it, use !, or set hidden.
  4. 4. What does the flag h+ in :ls mean?

    h means loaded but hidden, and + means modified. :wa writes it.

Summary

  • A buffer is text in memory, a window is a view of it, a tab page is a layout of windows.
  • :ls lists buffers; flags %, #, a, h, + tell you the state.
  • Switch with :b N, :b name, :bn, :bp and Ctrl-^.
  • :set hidden lets you leave modified buffers; :wa and :wqa save all.
  • :q closes the window, :bd removes the buffer, :bw wipes it.

Next: Windows and Splits