bd, bn, bp, ls, w, e, & me
I’ve been using Vim for decades, and not till recently did I realize, I’ve been using this tool quite back-assward.
I’d always thought of Vim as a file editor, and as
such, I’d open files —
vim foo.txt — and split
the screen when I wanted to work on another file
:spl bar.txt. I’d write my changes & quit
Vim, taking me back to the console (even though
I’d return to Vim not a minute later).
Now, I think in terms of buffers. And it’s been quite the shift in thinking now that I do that.
What does it mean to think in buffers? Well, first
off, an empty buffer is like standing at home
plate. If you type
vim with no arguments, you’ll
see what I’m driving at. Getting used to not
having a file in front of you was of zenith
importance for thinking in terms of buffers.
Now first base, from this blank buffer home plate,
is ‘checking in’ with your buffers. In ‘command
mode,’ this is achieved via
:ls — there you’ll
be given a listing of buffers that matter most.
I like to think of first base as a means of
getting my bearings after some hacking has made me
a bit uncertain on my whereabouts. A little like
pwd on the command line, you quickly remember
what the state of affairs is.
Back at home plate, one can open a file via ‘command
mode,’ with the
:e <foo.txt> command — or ‘edit’
for short. A quick
:ls will indicate the
addition of one buffer.
When this file is saved, I’m able to toggle
back/next to the blank buffer in a couple ways,
but in ‘command mode,’
:bn (buffer next) or
(buffer previous) will get it done. Quite easy to
With these four commands, the user experience is
like having tabs as we have it in other software.
The difference being that we need the
command to indicate such available tabs. But tabs
is really what it feels like, not what I prefer to
call it. They are buffers, and that’s such a nice
way of negotiating Vim, instead of doing so in
terms of files (or yes, even tabs, as Vim has
Writing and closing
One cannot ‘CRUD’ anything with just the four
mentioned commands. A buffer must be saved, with
the ‘command mode’
:w. But, instead of
which I did for years, just closing the buffer
with ‘command mode’
:bd (buffer delete) keeps us
in Vim and ready for more moving about via
:bd, we can also terminate other buffers
(they are numbered in
:ls, after all), so
:bd 4 will close the buffer with the
ID of ‘4.’ This is how one manages their
Vim is a rich editor with thousands of options and usage patterns. And that’s saying nothing of plugins. But for everyday hacking, this new way of moving around buffers offers a lucid experience, and one that’s easy to get up to speed with.
Update Feb. 2019
As it so happens, recently I’ve come to favor
:bd. I tend to want wipe the buffer
clean, so any remnants of one-time buffers are not
For example, say I have a file that I have been
working on named ‘foobar.txt.’ I can refer to it
quickly with just a substring match via
If I had this buffer laying around whereby it was
:bd, it can be the case that a
substring match would consider this deleted
buffer, even though I was done with it.
By performing a
:ls!, you can see your deleted
buffers. In contrast,
:bw will remove old
buffers for good and won’t make them candidates
for the above situation.