A buffer in vim is a currently open file, stored in memory. The files currently
being shown by vim are "windows", and buffers are files that vim has currently
in memory, so no all open files are displayed at the same time. If you run
vim file1 file2 file3 and then run
:ls, you'll see three files listed, but only
one file in the current window (file1). The other buffers are "hidden", more on
:h buffers to learn more about buffers.
Vim also opens those files (file1, file2 and file3) as args.
:args shows that
list of files, with the current file between
:prev is used to
cycle between them, and that list can also be edited, adding or removing files.
But as vim already opens those files as buffers, I use that instead.
The buffer's commands that I use the most are:
:lsto list them, or check which marks buffers have, like
#, for unsaved or last accessed buffer
:bpto cycle between them
:b3to open a specific buffer's number, here 3 is the buffer's number that I got from running
b *patternto open a file using a pattern, I use this one the most
That last option is great, if opens a buffer based on it's name, and can also
match the folder of the file (is part of it's name). So writing the following
and then hitting
wildmode) if it matched more than one:
After having the complete buffer's name, hit
There's also a special buffer in the buffer list marked with
#: that's the
previous buffer. I'm ofter switching back and forth two files, and running
:b#, or better yet
CTRL-^ will do that for me.
Then there's "hidden" buffers. A hidden buffer is a buffer not being displayed
in a vim's window, and by default, all those buffers are unmodified. So by
default, vim won't let you open another file or switch to another buffer, if
you don't save the current buffer first. You can change this by running
:set hidden, and now you can change between buffers or open files, without running
:wq, and any modified buffer will have a
+ next to their name in the list
I don't use this option as I prefer to just focus what I'm doing in the current
buffer. Although modified buffers will show a
+ mark next to them.
Finally, after a while of working, I start to have a lot of open files, and
searching or cycling through buffers starts to become tedious. What I usually
do is either kill the buffers I don't need anymore with
:bd (tab completion
also works for this one), or just kill all the buffers and open the one I was
currently working, like this:
I could specify a range for
:bd, like: kill from buffer 4 to 10:
but is easier using
%, meaning all buffers.