How I learned to stop worrying and love Vim buffers
Over the past six years I have used Vim with increasing exclusivity. I
have read a couple books (User
Vi) on Vim from cover-to-cover.
But I have always tried to use Vim as though it was a modern, tabbed
editor, even before it had
tabs. Once Vim 7 was
released with support for actual tabs, I ecstatically dove right in. I
vim -p and created my own MyTabLine function
displaying the tab number to make it easier to jump from tab-to-tab, for
8gt to jump to the eighth tab. But many tabs get unwieldy and
Vim quickly runs out of screen space to display them all. It became
apparent that I (and others, upon a cursory glance at the mailing list)
was trying to force a certain, modern, usage pattern on tabs that did
not fit the Vim editing pattern well.
In Vim, a buffer is a file that you have open in the editor (even if it’s not currently displayed), and a window is a view of a buffer. Well tabs in Vim 7 are really just views for windows. There isn’t a mechanism in Vim to tie a certain buffer to a certain tab (#1) which would fit better with the modern idea of tabs. This was finally impressed on me while watching Bram Moolenaar describe tabs: he doesn’t use them! They are really intended to group Vim windows without having to resort to multiple xterms or GNU Screen windows.
How the old guys use Vim
There’s a third opinion in the perpetual Emacs vs. Vim holy war written by one of those grumpy, uphill-both-ways, old guys. In an excellent writeup of his favored text editor he mentioned something that got me thinking: “Ed is for those who can remember what they are working on.” Certainly this is an exaggeration, but one that is rooted in the very real tendency of young people to succumb to the siren call of ADHD. (Not necessarily the actual disorder, but maybe just a faster-paced reality?) I realize that I lazily relied on the visible list of files that were currently open to know which files I was working on—even when I knew which files I was working on if I would just give it some thought.
So here are some tips on how to effectively use buffers in Vim, please drop more in the comments If you have ‘em. Also, never stop reading the built-in Vim documentation and online tips. Re-read a book on Vim every third year or so. Editors as old as this one have so many features you’ll never master them all, the more you keep trying the more efficient you’ll get at text-wrangling—and if you edit for a living, it’ll be well worth the time.
Vim Usage Tips for Young People
Start with the excellent Vim buffer FAQ! And search other Vim tips for ‘buffer’ for a lot of additional ideas.
Switch to a specific (N) buffer:
CTRL-^. Or switch to a
buffer by (partial)-name:
filena<CR> for a file named
filename.php, for example. I’m using a variant of tip
821 to quickly jump between
buffers by pressing
<F2> followed by a buffer number or name fragment:
map <F2> :ls<CR>:b<Space>
:bw to get rid of buffers you no longer need.
:set path to useful locations for your type of editing and
filename a lot. In fact, you can start Vim from the shell
to edit a certain file somewhere on your Vim path like this:
$ vim "+find filename".
Use the alternate buffer to quickly jump between two files:
CTRL-6 if you’re lucky) also you can do
:b#. My custom
displays the alternate buffer filename to remind me.
To open a bunch of additional files after you’ve already opened Vim use
:argadd files*.py. Try using Vim’s
browser or just
:cd - around the filesystem to get closer to
the files you need to add.
Map a key to
:args for quick reference when you need to see
all the buffers you’ve got open. (Mine is F1
#1 The plugin minibufexplorer actually does a very good job of mapping one (virtual) tab to one buffer–which may explain why it’s the second-highest rated script on vim.org. I highly recommend using it as a sort of “training wheels” for eventually using buffers without a visual aid, as it allows you to see all available buffers as you edit.