I don't understand buffers. When I open 3 files on the same tab and close my window, I'm generally annoyed to find out next time I open one of those files that there's strange swap files lingering and giving me pesky messages. But time and time again I read that these things are the productivity nirvana I'm missing out on and that tabs were made for the plebeians to use.

So I ask you, the Vim expert: what are the advantages of using buffers over tabs? I don't see how the difference could be profoundly different, but I would consider myself only at the beginner-intermediate level at operating Vim. Is :ls :b# really that much faster than gting around? I feel it must go deeper than this.

share|improve this question
    
For those learning Vim or coming to Vim heavily influence by other GUI based editors, I'd highly recommend first reading @Jonathan Brink's answer and then coming back to the top answer here. – icc97 Mar 30 at 19:08
up vote 234 down vote accepted

As ZyX said on #vim, this question sounds like "Why do Vim experts prefer tasty over warm?".

"Vim experts" don't prefer buffers over tabs: they use buffers as the file proxies they are and tab pages as the workspaces they are. Buffers and tab pages have different purposes so preferring one to the other makes no sense whatsoever.

The issue with buffers and tabs is one of confusion, caused by a combination of independent facts.

  1. Most "modern" text editors and IDEs use a tab metaphor to represent loaded files. That metaphor acts as an information system — it shows the user what files are opened and their state — and as an interactive device — it allows the user to manipulate (reorder, select, close…) those opened files. Despite their many limitations, tabs are everywhere and people are used to them and expect them everywhere.

  2. Vim introduced tab pages in 7.0 as a way for its users to create ad-hoc "workspaces". Nothing in their features, their specific options, their specific commands or their :help sections suggests that tab pages could or should be used as file proxies.

    Nothing except the name and the appearance of "tab pages", of course, which leads to much confusion.

  3. Without :set hidden, which is disabled by default and not very easy to find, Vim makes it impossible to switch to another buffer without writing the current one or abandoning its changes. New users, unaware of that option, have no choice but to turn to heavy windows use or to the closest "tab-like" feature they can find: tab pages.

"Tab page" is an unfortunate name choice for that feature, especially in an era dominated by the idea that reading documentation is a waste of time.

In Vim, tab pages are an abstraction built on top of windows, themselves an abstraction built on top of buffers. Each new level adds useful features but restricts your workflow.

The "buffer way"

With a buffer-based workflow, the files you are working with are distributed along a single dimension. You can cycle through your buffers, you can access a specific buffer by typing part of its name (with completion) or its number, you can alternate between buffers, you can target them pretty easily. There's basically no friction.

  1. Eight buffers open, only one visible:

    Eight buffers open

  2. Switching by number:

    Switching by number

  3. Switching by name:

    Switching by name

Buffers are Vim's file-proxies. If you think in terms of files, you think in terms of buffers.

The "window way"

With a window-based workflow, your "files" are both distributed along the same single "virtual" dimension as they would if you only used buffers and along two other "physical" dimensions. But the cartesian spaces in which those dimensions are found are almost completely separated: moving to another buffer still means "moving to another file" but moving to another window doesn't. The buffer that corresponds to the desired file may be displayed in that window but it could also be displayed in another one, maybe in another tab page, or not at all.

With windows, navigating between open files either becomes too complex or too simplistic, even with 'switchbuf' and :sb. Mostly because you are forced to use two sets of commands for what is essentially the same thing: accessing a buffer.

Windows have their use, as described below, but they don't have what it takes to replace buffers in anybody's workflow.

Here I am working on a Vim colorscheme. The two windows are different views of the same buffer: the top one serves as reference, with a table of the color codes used in the colorscheme, and the bottom one is where I work:

Working on a colorscheme

Windows are not designed as file-proxies and can't be made into ones: they are "containers" or "viewports" designed to offer you a view into a buffer. No more, no less.

The "tab way"

With a tab-based workflow, you essentially try to mimic the user experience you are used to from your previous editor while completely ignoring the very nature of Vim's tab pages. If we forget for a moment that this strategy is generally very unproductive, it is also impossible, just like with windows, to force Vim to adhere to that "one file = one tab" paradigm without loosing a lot of flexibility.

Still working with the same files as above, the tabline takes up a significant space for virtually no benefit. All my files and all my tabs are called javascript*.vim so I can't do 3gt and be confident I'll end up at the right place and it is impossible to reach a specific tab by name. Add to that the fact that its label can very well be the very unhelpful but perfectly logical [Quickfix List]… Since there is no practical way to tie a file/buffer to a tab page, you are basically left with only one practical way to navigate between tab pages/buffers/files: cycling.

And yeah, my tabline is clobbered with only 8 tabs, imagine if I had 20!

  1. Eight buffers open in eight tab pages (wrong)

    Wrong

  2. Two tabs for two specific tasks (right)

    Right

Tab pages are "containers" or "viewports" designed to contain one or more windows, themselves also "containers" designed to contain buffers.

In conclusion

"Vim experts" (let's assume I can speak as if I was one) don't prefer buffers over tabs: they just use Vim as it was designed and are perfectly comfortable with that design:

  • "Vim experts" have 2, 30 or 97 buffers loaded and are very happy they don't have to deal with spatial distribution;

  • when they need to compare two files or work in one part of the current buffer while keeping another as a reference, "Vim experts" use windows because that's how they are meant to be used;

  • when they need to work for a while on a separate part of the project without messing with their current view, "Vim experts" load a brand new tab page.

share|improve this answer
6  
For some additional links see my Use buffers effectively post – Peter Rincker Nov 3 '14 at 14:27
6  
@DavidEG, having 20 buffers is not problematic at all so there's really no hard need for a plugin. On the other hand, 20 tab pages — whether they are convincing file-proxies or not — can't fit on most screens, plugin or not. – romainl Nov 3 '14 at 16:00
7  
As a “Vim expert” I can say that over 4 hundred buffers “open” (really “listed, but unloaded, except for a few ones”) is a regular situation when I deal with project like NeoVim (I simply open all *.c, *.h, scripts/* and test/**/*.lua files). Given that my terminal is only 239 columns wide “one file per tab” approach is impossible to use. – ZyX Nov 21 '14 at 16:39
3  
And given that there is a number of plugins (Command-T, …) that makes switching between buffers and/or files easier using tabs for any relatively large project makes no sense. And neovim with ≈500 “interesting” files is big project, but not the largest one. When you face the necessity of dealing with such projects you always use some kind of search to navigate it (file/tag search with Command-T and friends, various ways to go to symbol definition) and thus you have absolutely no reason for using tabs this way: in any case you will not be using tab-bound functionality to navigate the project. – ZyX Nov 21 '14 at 16:40
3  
@BenjaminRH, I eat my own dog food. – romainl Mar 22 '15 at 9:17

I used to keep every buffer in a separate tab, but I grew tired of constantly gt and gT-ing around everywhere.

I also felt that buffers were too difficult to manage.

Here are some techniques that totally changed my earlier opinion:

Here is my typical workflow:

  • Open vim, and use edit (usually with a regex) to open a buffer
  • Realize I need to open another file. Use "edit" for that as well. If I want to toggle between this buffer and the currently open buffer I will use "sp" or "vsp" to open it in a separate window.
  • Repeat until I've got the 3-5 files that I will be switching between using the techniques in the above bulleted list to fly between your buffers.
  • If I want to "start over" with my buffers, just close vim and re-open.

I felt that after a week or so of forcing these new patterns, it became much easier to visualize which buffers I had open, and how to get to any one of them in only a few automatic strokes.

share|improve this answer
4  
It's a shame that kind user/newbie friendly explanations like this get about 3% of the upvotes of very opinionated, derogatory overly complex answers such as the top voted one here. I didn't even know that gT was the command to switch tabs, I'd been searching for the replacement for ctrl+tab. So thank you for actually genuinely helping a new user rather than just making them feel stupid. – icc97 Mar 30 at 12:11
1  
I have to say that my comment is unfair to @romainl's answer, he was very happy to answer questions I had about it. But certainly as someone trying to learn Vim your answer is much easier to understand, but his answer is more complete once you actually know a bit more. – icc97 Mar 30 at 18:59

The downside of tabs is that you can only see the contents of one at a time. So if you use them like in a browser, you're losing out on viewing multiple buffers side by side, or even viewing separate parts of the same file in splits. Therefore, many recommend to use tabs only to segregate different workspaces (e.g. have one for a Java project, another for a todo list, a third to hack on a script on the side).

The problems you describe make it appear that you're using Vim wrong. Either have (mostly) a single, dedicated instance. Then, buffers that become hidden will simply "reappear" if you re-edit them (and you can now use the buffer list to recall them), and there won't be swap file messages. Or, use separate Vim instances per project / file / edit session, but then make it a habit to fully :quit each instance when you're done with the file.

share|improve this answer
    
I use splits occasionally. I wasn't aware they were considered 'using buffers'. It's a mysterious concept to me really. – 2c2c Nov 3 '14 at 8:57

Another tip, when using the buffer name as the argument to :buffer, you don't have to specify entire name. However, if more than one buffer matches the given argument then the buffers won't be switched.

Any fragment of the buffer name can be used to match. For example, if you have the buffers request_manager.java and queue_manager.java then :buffer que matches or :b que` both of them, but will switch to queue_manager.java as it matches at the beginning.

share|improve this answer

I use tabs, Ctrl-P and Vim sessions in my workflow and have for over a year now:

  • I have ) and ( mapped to "go to next tab" and "go to previous tab" respectively. tn opens a new tab. I also make use of tabm to help keep things organized.

  • I use Vim sessions for groups of files relating to the current story/bug I'm working on, usually done by category. These sessions get overwritten during the course of the process.

  • I have yet to find anything better than Ctrl-P, but it does take a bit to process all the files for finding.

share|improve this answer

Add these to your .vimrc and start loving buffers:

:nnoremap <Tab> :n<cr>
:nnoremap <S-Tab> :N<cr>

That way you can cycle forward/backward through them in normal mode via Tab/ShiftTab.

share|improve this answer
3  
Don't do that. You'll lose the mapping for <C-I>. Map <C-Tab> instead if you really want to. – jeyoung Nov 23 '16 at 17:06
1  
Also, :n and :N relate to the argument list, not open buffers. You'd want :bn and :bp (:bnext and :bprev). tpope's unimpaired provides mappings ]b and [b for this (and other good stuff) if you want. ( and ), or <left> and <right> arrows, would arguably be less useful keys to override than tab, if you really want a short mapping. – Vaz Dec 9 '16 at 21:56
    
@jeyoung agreed - it also makes much more sense to use Ctrl + Tab as that's what most other GUI editors and browsers use. – icc97 Mar 30 at 18:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.