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 :bp
(buffer previous) will get it done. Quite easy to
remember.
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 :ls
command to indicate such available tabs. But ‘tabs’
is acutally 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
those, too).
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 :wq
,
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
:e
, :ls
, :bn
or :bp
.
With :bd
, we can also terminate other buffers
(they are numbered in :ls
, after all), so
actions like :bd 4
will close the buffer with the
ID of ‘4.’ This is how one manages their
session spaces.
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
:bw
over :bd
. I tend to want wipe the buffer
clean, so any remnants of one-time buffers are not
searchable later.
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 :b ba
.
If I had this buffer laying around whereby it was
deleted via :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.