Vim buffers

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 that later...

Check :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 [], :n and :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.

buffer's commands

The buffer's commands that I use the most are:

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 will auto-complete with the buffer that matched that pattern, or with a list (I think this depends on what option you have for wildmode) if it matched more than one:

:b *partialPath*partialFilename*

After having the complete buffer's name, hit to access it.

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.

hiding buffers

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 from :ls.

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.

deleting buffers

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: :4,10bd, but is easier using %, meaning all buffers.