Buffer Management
[!NOTE] This chapter demystifies Vim’s core data structure: the Buffer. Understanding the distinction between a Buffer (what you edit) and a Window (where you view it) is the key to unlocking Vim’s workspace management.
1. What is a Buffer?
In a conventional text editor, closing a tab closes the file. In Vim, this is not true.
A Buffer is simply an in-memory representation of a file. When you open a file in Vim, you are loading it into a buffer.
- A Window is a viewport onto a buffer.
- You can have multiple windows displaying the same buffer.
- You can have a buffer that is not displayed in any window (a “hidden” buffer).
Understanding this separation is crucial. When you type :q to close a window, the buffer remains in memory. You haven’t “closed the file” in the traditional sense; you’ve just closed your view of it.
2. Listing Buffers
As you open files during a session, they accumulate as buffers. To see all currently open buffers, use the list command:
:ls:buffers(Synonym for:ls)
This will output a list that looks something like this:
1 %a "main.go" line 12
2 #h "utils_test.go" line 45
3 "README.md" line 0
Deciphering the :ls output:
- Number: The unique ID assigned to the buffer (e.g., 1, 2, 3).
- Flags:
%: The buffer in the current window.a: The buffer is active (loaded and visible in a window).h: The buffer is hidden (loaded in memory, but not visible in any window).#: The alternate buffer (the one you were previously in).+: The buffer has unsaved modifications.
- Name: The file path.
- Line: Your cursor position in that buffer.
3. Buffer Navigation
You don’t need a file explorer sidebar to switch files in Vim. Once a file is in memory as a buffer, you can switch to it instantly.
| Command | Action |
|---|---|
:bn or :bnext |
Go to the Next buffer in the list |
:bp or :bprevious |
Go to the Previous buffer in the list |
:b <number> |
Jump directly to buffer ID (e.g., :b 3) |
:b <name> |
Jump by name. Supports partial matches (e.g., :b read jumps to README.md) |
Ctrl-^ or Ctrl-6 |
Toggle back and forth between the current (%) and alternate (#) buffer. This is incredibly useful for flipping between a source file and its test file. |
[!TIP] Fuzzy Finding Buffers: While
:b <name>is powerful, most modern Vim users rely on a fuzzy finder plugin likefzf.vimorTelescope(in Neovim) mapped to something like<Leader>bto instantly search and switch buffers without typing exact names.
4. The Interactive Buffer Model
Buffers vs Windows Simulator
Observe how closing a window (viewport) does not delete the buffer (memory). Click a buffer in memory to load it into the active window.
RAM (Buffers List)
Screen (Windows)
5. Deleting Buffers
When you are completely done with a file and want to remove it from memory (and remove it from your :ls list), you delete the buffer.
- Command:
:bdeleteor:bd - Action: Unloads the buffer from memory. If the buffer is displayed in a window, that window is usually closed as well.
To delete a specific buffer by ID: :bd 3
To force delete a buffer with unsaved changes: :bd!
[!WARNING] By default, if you modify a buffer and try to switch to another buffer (
:bn), Vim will stop you and throw an error:E37: No write since last change. You must either save the file (:w) or force the switch (which loses changes).The
hiddenSolution: Most power users addset hiddento their.vimrc. This allows you to switch away from modified buffers without saving. The changes remain safely in memory (as a hidden buffer) until you are ready to write them to disk.