Why Vim tabs are different from any other editor
April 24, 2012 in SoftwareAs you likely know, recent versions of Vim have tabs. If you’re coming to Vim from a different tabbed editor (and of course you are), you might decide to use this feature as you’re accustomed to. Well, guess what – you’re doing it wrong.
First off, you definitely can use tabs to open files in them. And while you’re just editing separate files, you’re very much ok. However, as you try to use even moderately advanced features of Vim, such as quicklists, or some filesystem plugin like NERDTree, you find out that they are always confined to a single tab.
While at first this will seem a weird design flaw, it’s actually not.
Windows, tabs and… windows in Vim
The base element of Vim layout is a window (which is not an OS window, which contains all of Vim). A window is essentially a viewpoint into a buffer, and a buffer is, generally speaking, a file. Each buffer (open file) has zero or more windows (viewpoints) attached to it. To edit a buffer, you bring it up into a window using :edit
, or one of the b
-commands. And each file can be open in one and only one buffer, thus edit
ing a file that’s already open in a buffer is equivalent to opening that buffer.
Before there were tabs, Vim contained one or more windows, being in itself a tiled window manager.
So where do tabs fit in? Imagine this. You’re editing two files in splits, side-by-side. And then you need to search-and-replace something across the project, so you’ll want to open a quick fix window. But the quick fix window will ruin your editing layout, and then you’ll have to recreate it again.
Tabs were introduced to have more than one window layout inside the same Vim instance. Tabs share every setting of Vim’s environment, except for the window layout.
So, using our example you can open a new tab and do the search-and-replace there, being able to switch back to the original layout at any time! Convenient, huh?
But I just want a tabbed file editor!
Vim had multiple file editing before your old editor was even born. You can have one window and as many buffers as you want and switch between them at any time.
By default, Vim does not allow buffers to be open in the background. To enable this feature, add the following line to your .vimrc
.
set hidden
One great advantage of this approach is that you never have to worry about closing files, and you never ever “run out of tabs”. And it’s less taxing on the system, which matters if you edit tens of files simulataneously.
To make switching buffers easier, install LustyJuggler. Believe me, it’s far better than a tab bar, especially if you don’t want to use the mouse.
And if you really need a visible tab bar to click on (you pervert), install MiniBufExplorer.
Liked the post? Treat me to a coffee