The Overdue Intro to Vim Buffers
Buffers are not files or tabs or windows. Buffers are references to files.
A buffer is the in-memory text of a file. A window is a viewport on a buffer. A tab page is a collection of windows. source :help buffers
How do you view buffers?
:ls
How are buffers created/used?
When you you open a file, a buffer is created. If you close and reopen the file, the same buffer is used.
If you create a new file, a new buffer is created. If you close the new file
without writing, the buffer will remain. If you close the new file and discard
everything, :q!
, the buffer will be closed too.
How do I understand/use buffers?
:ls
will list your buffers prefixed with buffer IDs and flags.
:ls
4 a + "~/Desktop/blog.md" line 21
7 #h "~/Desktop/your.mom" line 0
8 %a + "[No Name]" line 3
The first column is the buffer ID. You can use this to open the associated
file. Use :buffer N
where N
is the number of the buffer. You can do this
immediately after :ls
.
To see your.mom
,
:buffer 7
Flags
%
- currenta
- activeh
- hidden+
- modified
When :wqa
doesn’t work
"~/Desktop/blog.md" 55L, 1173B written
E141: No file name for buffer 8
Press ENTER or type command to continue
If you temporarily put something in a new file, jumped to another file, and then tried to quit Vim, you may get something like this. It’s a failsafe to make sure you are throwing away changes you meant to save.
To resolve this, open the associated file with :buffer 8
. Then save :w
whatever
or discard :q!
.
CtrlP - mo better
I often use ctrl-p and jump from files to mru files cause I’m lazy. This is pretty good, but it might be even better to stop between the two at buffers. mru files shows all sorts of stuff, including previous sessions. When you want to focus on just the files you’ve been editing in this session, use the buffers option. Remember, even when you close a file, the buffer remains. That means, you can work on file1, open file2, bounce around through code for a while, then get back to file1 through the buffer reference.