330

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 are 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.

Enbugger
  • 314
  • 2
  • 6
  • 23
2c2c
  • 4,694
  • 7
  • 22
  • 33
  • 14
    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](http://stackoverflow.com/a/26745051/327074) and then coming back to the top answer here. – icc97 Mar 30 '17 at 19:08
  • 3
    Don't worry too much about it. I use tabs as my primary way of navigation among files. As you say, <#>gt is easy to type. To make it even easier to use, I customize my tabline data to include the tab number. If I need to view more than one buffer at a time, I split a new window within the tab. Despite what @romaini claims, this way of using Vim makes me very productive because it fits my personal metaphors. Note also that in a 37 (so far) year career as a programmer, I've not yet had to have more than five or six files open at once - and that very rarely. Do what works for you. :) – Tony Oct 14 '18 at 13:52
  • Do what works. Some people use a lot of files in a session so buffers matter a lot more. Even [Bram Moolenaar says "organize them as you like."](https://youtu.be/eX9m3g5J-XA?t=4506) Having 20 tabs open isn't the right way, but tabs for a few files isn't the end of the world. Bram's solution to managing a lot of files is opening several terminal sessions. He says thats how he does it, but not that this is how everyone should do it. – craft Jan 19 '19 at 03:37
  • The single most obvious difference that doesn't seem to be highlighted properly is that if you stay in one tab you can view multiple buffers at the same time with by `:split`ting into "windows". If you have all your buffers (files) in separate tabs you won't get that simultaneous view. I'd recommend learning vim using 1 tab to start with and getting used to splits. – NeilG Oct 23 '19 at 22:54

10 Answers10

637

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 losing 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.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 14
    For some additional links see my [Use buffers effectively](http://stackoverflow.com/a/21338192/438329) post – Peter Rincker Nov 03 '14 at 14:27
  • I must be a vim heresy because I use tabs quite a lot and I don't run into any problems :D Having 20 tabs is (almost) as problematic as having 20 buffers, both can be addressed with some plugins :-) (♥ ctrlp) – DavidEG Nov 03 '14 at 15:37
  • 7
    @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 03 '14 at 16:00
  • @DavidEG How do you switch between tabs? Multiple `gt`'s? With a count: `4gt`? or do you use something like CtrlP? If you are primarily using a fuzzy finder then you are essentially doing a quasi buffer-centric approach. I imagine if you scale this up from 20 files to something like 50+ and not use any switching plugins you will come to understand the different needs tabs and buffers fill. – Peter Rincker Nov 03 '14 at 16:05
  • I have some mappings like alt+l/h for gt/T, alt+ for gt, and a very useful altgr+t to switch to last tab. When things grow I use a ctrlp extension (https://github.com/vim-scripts/CtrlP-SmartTabs) for fuzzy finding. Indeed is similar to a buffer-centric approach but I tend to keep a particular window configuration in each tab, and I don't want to lose that. Normally I use buffer for jumps like gf, ctrl+], ctrl+t, etc. – DavidEG Nov 03 '14 at 16:29
  • @DavidEG I personally can only keep so much in my head and can only do a shockingly low number of tasks at a time. Therefore I usually only have 1-2 splits open and rarely use tabs. I rely on Vim's native navigation tools like, `:b`, tags/cscope, args/quickfix lists, `gf`, marks, etc. I use plugins to fill any voids like a fuzzy finder for quickly opening deeply nested files and [projectionist](https://github.com/tpope/vim-projectionist) to handle intera-project navigation. I find this approach makes for an uncomplicated workflow. – Peter Rincker Nov 03 '14 at 18:03
  • The one thing that tabs and windows are good for is spatial organization. I can organize viewports for multiple related buffers in close proximity to each other and then switch by prev/next tab or up/down/left/right window, rather than by number or retyping filenames. I don't think I can do the same with just buffer management. – Kache Nov 03 '14 at 19:16
  • 3
    @Kache, yes, that's exactly the core of my point: you can use windows and tab pages for what they are but not as file-proxies. So the argument is not whether people should use **tab pages or buffers** it is whether people should use **tab pages as file proxies or not.** – romainl Nov 03 '14 at 20:21
  • 21
    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
  • 1
    Which theme are you using in these screenshots? I must have it! – BenjaminRH Mar 22 '15 at 03:22
  • *"Despite their many limitations, tabs are everywhere"*. You seem to have listed two: 1. If all the files have the same name but in different directories, 2: If you have more than 20 tabs open. Both of which are dealt with in an editor like Sublime Text. What are the other limitations? I'm genuinely interested as I want to switch from ST to Vim but I'm getting frustrated by the complexities. – icc97 Mar 30 '17 at 11:56
  • 2
    @icc97, I'm not aware of a single program that deals with those two limitations in a convincing manner, Vim included. The tab titles are always garbled in one way or another (only the filename or what Vim does which is horrible), the tabs themselves end up either stacked and unreadable or out of the viewport and unreadable, the user is forced to use the mouse to hover over the tabs to get more information. Tabs suck hard. – romainl Mar 30 '17 at 12:21
  • Not saying this is perfect, but it's the best example I've seen, For 1) Sublime Text appends the parent directory: http://imgur.com/a/c5o9A. For 2) ST handles this with basically just `Ctrl + P` and fuzzy matching as you type the file name (then it doesn't really matter where the tab is). This I think is similar to switching between buffers in Vim. – icc97 Mar 30 '17 at 12:35
  • 2
    1) is pretty standard actually but corner cases are easy to come by. 2) IS not really "handling" it, it's more like "avoiding" it. Sublime's Ctrl+P (like its predecessors and its imitators) is itself a much much better alternative to lists, trees, and tabs which all have numerous usability issues. Buffers is definitely THE way to go. In Vim and elsewhere. – romainl Mar 30 '17 at 14:13
  • Buffers and windows are an amazing combination, but I can't really figure out what tabs are *good for*, in the vim context (elsewhere, they're useful because there's no other option). I guess they make sense if you're trying to multi-task, but [multi-tasking is a myth](https://www.psychologytoday.com/au/blog/creativity-without-borders/201405/the-myth-multitasking)... – naught101 Mar 01 '19 at 01:50
  • @naught101 tab pages are windows layouts. If your workflow involves several windows laid out in a specific way then your workflow involves tab pages because you may want a second similar layout to switch to, or a different one. – romainl Mar 15 '19 at 11:11
  • My minimalist [tabline plugin](https://github.com/lukelbd/vim-tabline) solves many of your gripes: (1) if there are too many tabs, ellipses are placed at the beginning and end of the tabline, (2) long filenames are truncated at the beginning+end equally, which in my experience makes them easier to identify, (3) the tab title is always the title of the "main window"; windows with buffers belonging to certain ignored filetypes, e.g. `help`, are not used for the tab title. I also have a `` [dotfiles](https://github.com/lukelbd/dotfiles) map that opens a popup window for tab-switching. – Luke Davis Jul 07 '19 at 06:07
  • @romainl - what Vim theme do you use? I really like it. – Shuzheng Jun 02 '20 at 09:25
  • @Shuzheng, I use my own: [Apprentice](https://github.com/romainl/Apprentice). – romainl Jun 04 '20 at 06:30
  • @romainl - Wonderful, do you have a similar theme for Tmux or iTerm2? – Shuzheng Jun 04 '20 at 06:35
  • @Shuzheng, I don't have anything for tmux but there is a link to a repo with lots of terminal themes ported from apprentice in the last section of the README. – romainl Jun 04 '20 at 06:41
  • I use tabs heavily, mainly for the benefit of having effective visual presence across files. The primary benefit of this is when jumping back to an open vim from a week ago and being able to pick up right where I left off. The positioning of splits, the scroll offsets within them, the git state, everything helps for this, and this kind of metadata state is worth its weight in gold. Although in typical use I could be perfectly content to toggle across buffers, it really is quite limiting if I have 500+ columns and 200 rows worth of terminal to work with. – Steven Lu Nov 08 '20 at 22:10
  • The more screen you have on your battle station, the less manual navigation you have to endure. I'm trying to avoid RSI these days. My key binds are F2 to move to previous tab and F3 to move to next tab. What happens for me is I open up splits and once I need to work on the backend as well, that could be a new tab, and now I have, say 4 frontend files open on tab 1, and 4 backend files open on tab 2. But what gets tricky is when I code something on tab 1 and want to refer to something on tab 2. For this, I have a yank bind so I can type y, F2 on a backend file and yank that split window... – Steven Lu Nov 08 '20 at 22:12
  • ... into the first tab so now I can refer to everything. In my experience I have definitely found it to be easy to leave a mess of tabs and have the same file opened multiple times across many tabs. Sometimes this is necessary, so I would never want to prevent this. I am pretty certain that if I were to add a key map to `:tabclose` this would never bother me, considering how easy it is to re-open buffers into whatever split configuration that I desire. Anyway, my point is if you use a large screen or many screens you owe it to yourself to make it easy to have many splits open. It helps me... – Steven Lu Nov 08 '20 at 22:15
  • What I'm trying to say is I encourage all of you who have gotten used to the buffer centric workflow to really search your soul about why it's better than using tabs. Because the argument that tabs are so much more difficult and painful to navigate is a completely invalid argument from my perspective, however, I will concede that my perspective is only attainable after having spent a good amount of time years ago optimizing my key maps to allow me to efficiently traverse splits and tabs. I just feel bad for anyone who doesn't want to leverage visual memory for productivity. – Steven Lu Nov 08 '20 at 22:20
  • Circling back though. I am completely in agreement with the argument *"Dont get in the habit of using X number of tabs to open X number of files"*. There's just no benefit to doing that aside from gaining a positional ordering of your buffers, which is really more limiting than not. What I'm saying is people should try to use splits more... – Steven Lu Nov 08 '20 at 22:31
  • 2
    @StevenLu have you actually read my answer? – romainl Nov 09 '20 at 07:24
  • I definitely did, and I've re-read it again now on your prompting. The answer is simply heavily biased towards the notion that if you don't primarily use buffers, you're "using it wrong". I cannot agree with this. In particular, statements such as: "[Windows] don't have what it takes to replace buffers in anybody's workflow." My wall of comments is a direct response to statements like these. Nobody (least of all vim) forces you to use spatial layouts. HOWEVER, when you argue against use of windows, you're preventing new/novice users from experimenting and getting the most out of the software. – Steven Lu Nov 09 '20 at 20:15
  • 6
    And you should re-read it again because you managed to miss the point twice. Here is a summary, just for you: **buffers, windows, and tab pages all have their place and favouring one to the others on principle alone makes no sense**. If that answer has any bias, it would be a bias against misusing either of those things, not against using them. – romainl Nov 09 '20 at 20:39
  • 2
    I largely agree with this very good answer. There's just a little point to add. _Sometimes_ it's not about saving keystrokes and even not about efficiency. Buffer-only usage has an intellectual impact as you need to recall names which may be undesirable while programming. Sometimes it's a big relief to visually see other buffer names (without extra `:ls`). – steffen Apr 30 '21 at 09:56
  • The funny thing, when I'm in `emacs` using buffers sounds okay or fine to me. Well, there's no tabs there after all. No tabs in a way they exist (behave) in `vim`. But when I switch to `vim`, I can't help but "regress" to using tabs. Maybe that has to do with my eye sight and the screen size (at the moment the maximum extents are 92x25, and I don't feel comfortable with much greater values). And using splits... that's what I prefer to avoid. I use them in cases where there's no alternative. Or when splits sounds like the best option in spite of the downsides... – x-yuri Nov 29 '22 at 08:57
  • ...You might probably call it claustrophobia. I wonder if you're comfortable with a buffer 12 lines high. Or 40 columns wide. To edit a source file. Of course you don't want to have many tabs open. But within one tab there's no easy way to quickly switch between more then two buffers (`Ctrl-^`). Unless you use splits. But with tabs there is (unless you have many tabs). Especially if you map `:tabn`/`:tabp` to `Alt-j/k` or something. There's even a way to move tabs (`:tabm`), which I mapped to `Alt-Shift-j/k`. Which I believe is not possible with buffers... – x-yuri Nov 29 '22 at 08:57
  • ...That makes me think that I use tabs for cases where you'd use splits. But since I "can't" use splits, I use tabs instead. Which brings up the question, "Aren't you too stuck to what vim is designed for, and as such miss some opportunities?" In my turn I must admit that I haven't yet tried to `:set hidden`. And I need to put more effort into using buffers more. But well, `:set hidden` makes it harder to see if you have any unsaved buffers. Another thing that disappoints me is that when I do `:lvimgrep` it adds to the buffer list possibly a lot of files I don't usually need... – x-yuri Nov 29 '22 at 08:58
  • ...And yes, I agree with Steven Lu that your answer sounds biased towards using buffers. Also some sort of migration guide might be in order. The materials I saw on the topic, all of them leave some questions open. And to use buffers... for a better experience I believe you need `fzf.vim` or something. You can of course use `:b {bufname}`, but that just sounds less effective. – x-yuri Nov 29 '22 at 08:59
  • @x-yuri, I have stopped using fuzzy gyzmos for navigation many years ago because they didn't provide any noticeable benefit to me. I have been relying on well tuned and well understood built-ins since then and I am very happy with what I have: less setup, less third-party dependencies, less FOMO, better portability… As for being biased toward buffers, this answer and pretty much all my other answers are **all** biased toward learning and using the defaults/basics, which too many vimmers tend to avoid/shun for silly reasons. Make that what you will. – romainl Nov 29 '22 at 09:25
  • Don't take me wrong. The answer is good, and I don't want to argue. Can you possibly address the raised issues? (1) Let's say you want to shut down the machine. For that you need to quit all your `vim` instances to save any unsaved files? I usually have a couple of them, sometimes many. (2) Don't you think that `:lvimgrep` clutters the buffer list? Of course you can still switch the buffers, but the more the buffers, the harder it is to switch. Or not? (3) Do you open all the files at the beginning? Because otherwise you have to remember if you opened a file or not. – x-yuri Nov 29 '22 at 17:19
  • None of that is related to this question or this answer. Ask separate questions or use a different channel, like reddit or irc. – romainl Nov 29 '22 at 18:23
  • `too many vimmers tend to avoid/shun for silly reasons` This sounds likely. A [case](https://vi.stackexchange.com/questions/458/how-can-i-reload-all-buffers-at-once/39820#39820) I ran into with `hidden` buffers. – x-yuri Jan 17 '23 at 21:01
156

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 :e (usually with a regex like :e src/**/F*Bar.js) to open a buffer
  • Realize I need to open another file. Use :e 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.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • 41
    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 '17 at 12:11
  • 15
    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 '17 at 18:59
  • 8
    I think @romainl's answer is extremely useful, and gives us a clear picture of how buffers, windows, and tabs are designed, and how they're meant to be used. Even still, I think Vim users jump at the chance to answer questions in a [you don't grok vi](https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim/1220118#1220118) fashion, and that can be frustrating. – keyofnight Dec 16 '18 at 10:01
  • 3
    Can't remember where I got it from, but `nnoremap b :ls:b` is quite nice for quickly switching buffers, as it shows you a list of the currently open buffers. Also, partial names are accepted (as long as there is only one match). – naught101 Mar 01 '19 at 01:40
  • 1
    This method also has the really awesome benefit of vim's auto-complete (default, no additional plugins). Since you're using multiple buffers, when you are in insert mode and do a `ctrl N` or `ctrl P` (P is what I usually use), it will give you a list of words to finish what you were typing... Its smart based upon your current buffer, ones in splits, ones you were just looking at, and every other open file! – g19fanatic Sep 12 '19 at 11:00
23

Tosses 2c into the pile.

TLDR; :b *part-of-filename* is the best way to find a file you need in the buffer list i.e. it is FASTER and has LESS cognitive load than buffer numbers, tabs, or windows for tracking files.

It's nothing for me to have 30 buffers open (I.e. I haven't been housekeeping), and the beauty of buffers used-well is that it doesn't slow me down at all. In fact, it speeds things up when four days after I opened the file I need it, call :b *part-of-filename* and it magically appears, impressing co-workers and toady collectivists alike.

Buffers are for files.

To be effective:

  • Open an important first file from a devilishly well chosen root directory
  • Open subsequent files with :e
  • Use ls ALL the time when you first start to get a good mental model (you can't grok what you can't see, mentally or literally)
  • Never :q, it blows
  • Enter :b into your muscle memory
  • :b1 is good for the first file you know you opened, otherwise numbers and letters get clumsy quick
  • :b# is good for switching to your last file, which is a common need
  • :bd# is good for when you've switched to a temp file, done what you needed to do, switched back with :b#, and now want to close that temp file
  • :b *part-of-filename* is otherwise the best way to find a file you need in the list i.e. it is FASTER and has LESS cognitive load than buffer numbers, tabs, or windows for tracking files.

The only annoyance of :b *part-of-filename* is that sometimes you haven't opened the file yet, and you need to go back and :e path/to/full-filename first.

Tabs are for differentiating truly unrelated files.

Or keeping a particular windows layout handy (disclaimer: I've never used it for this myself).

Or for files rarely used, but predictably needed. For me, that's usually a commitMessage file that I annotate as I work so I don't have to do too much thinking when it comes time to make a commit. gt is faster than :b com<enter> (if you're feeling lucky, otherwise :b com<tab><enter>)

  • :tabe commitMessage
  • gt or gT also a muscle memory favorite

Window splits are for visually comparing information

Or having immediate access to important information (truthfully, unless that info is somehow something I need to live update with :e i.e. a log file, I usually just pull the content into the current file and deal with it there).

  • :vsp or C-w v opens a vertical split i.e. left | right, then use :b or :e to get the file you want
  • :sp or C-w s open a horizontal split i.e. top / bottom
  • C-w C-w i.e. double Ctrl-w, rotates you around the available windows
  • C-w c close current window
  • C-w o close all other windows, keep current ONLY
Paul Parker
  • 1,163
  • 12
  • 23
  • Incredibly useful answer, should have way more upvotes. Thanks for the tips, especially the insight into your workflow with `:b#` and `:bd#`! – Jackson Holiday Wheeler Jun 14 '20 at 16:37
  • Why "never `:q`"? – Jonathan.Brink Dec 10 '21 at 01:51
  • 1
    @Jonathan.Brink because it breaks the workflow. When working on a project you build up a buffer list of files that you're working on. One careless `:q` and you find yourself on the command line and having to open up all the relevant files again, one by one. `:e` requires specificity, i.e. you have to remember the exact path and filename, but `:b` requires only a partial memory of what the filename looks like. – Paul Parker Dec 11 '21 at 02:22
13

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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • I use splits occasionally. I wasn't aware they were considered 'using buffers'. It's a mysterious concept to me really. – 2c2c Nov 03 '14 at 08:57
  • 2
    As an interesting rediscovery of tabs, [tabman](https://github.com/kien/tabman.vim) can generate a NerdTree-like side panel, which details all buffers as they were displayed in each tab. – llinfeng Jan 24 '19 at 01:34
7

Another tip, when using the buffer name as the argument to :buffer, you don't have to specify entire names. However, if more than one buffer matches the given argument, 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 or :b que matches both of them, but will switch to queue_manager.java as it matches at the beginning.

A. Alencar
  • 141
  • 2
  • 8
Julius Martin
  • 159
  • 2
  • 8
4

Tabs and Buffers are two different standards in Vi. Read these three definitions:

A buffer is the in-memory text of a file
A window is a viewport on a buffer.
A tab page is a collection of windows.

Read this article for more https://joshldavis.com/2014/04/05/vim-tab-madness-buffers-vs-tabs/

Gentrit
  • 51
  • 1
3

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.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
1

I load "selected" buffers as tabs to quickly (TAB/S-TAB) toggle between them. The framework of workspaces fits here as for me buffers VS tabs is mostly the visibility thing. I can pop important/work files in windows and tabs and hide the ones I don't currently need to utilize in the background on the fly without having to remember paths or take time to search and load them up again once the need arises. This allows for handling several tasks or projects in one VIM session, I guess this used to be important in low-memory machines but is also good for concentrating all editing tasks under one application frame. I also have buffer shifting shortcuts set to Ctrl-Right/Left so I can quickly shift through various buffers as well.

Bottom line, one can only split up to some windows for his uses as much as screen estate goes, but one can hold multiple windows settings in several tabs thus expanding one's workspace and improving workflow allowing the convenient division of complicated tasks revolving more than one file.

For the swap files, you can tell VIM to keep all of them in one folder of your designation. For this use :set directory.

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
elig
  • 2,635
  • 3
  • 14
  • 24
1

I would like to suggest a brillent implementation from a good number of years ago: kien/tabman.vim. It clarifies the following:

  • One can have as many buffers that are carefully hidden, somewhere;
  • By design, tabs are meant to display bufferes in creative ways.
    • With some proper tabline plugin, one can display all the hidden buffers at the top row (tabline);
    • Per my experience with vim-airline, the tabline will show very few relevant information when I create a new tab.
    • Two tags will occupy the tabline slot, side by side, wasting the rest of the horizontal spaces
    • Worst still, I no longer have any idea of what are the buffers that are hidden.

It has been a wonderful rediscovery of this magic plugin, which should have been staying in my Vim configuration for a good number of years as well. While I would continue to look for some thing that also displays all the hidden buffers, TabMan is my superman when it comes to having a bird's eye view of how buffers were arranged across different tabs.

llinfeng
  • 1,316
  • 1
  • 15
  • 39
-2

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.

sjas
  • 18,644
  • 14
  • 87
  • 92
  • 6
    Don't do that. You'll lose the mapping for ``. Map `` instead if you really want to. –  Nov 23 '16 at 17:06
  • 8
    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 `` and `` arrows, would arguably be less useful keys to override than tab, if you really want a short mapping. – Vaz Dec 09 '16 at 21:56
  • 1
    @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 '17 at 18:55