Buffers, windows, and tabs

If you’ve moved to Vim from an editor like Notepad++ or TextMate, you’ll be used to working with the idea of tabs in a text editor in a certain way. Specifically, a tab represents an open file; while the tab’s there, you’ve got an open file, as soon as you close it, it goes away. This one-to-one correspondence is pretty straightforward and is analogous to using tabs in a web browser; while the page is open, there’s a tab there, and you have one page per tab, and one tab per page.

Vim has a system for tabs as well, but it works in a completely different way from how text editors and browsers do. Beginners with Vim are often frustrated by this because the model for tabs is used so consistently in pretty much every other program they’re accustomed to, and they might end up spending a lot of fruitless time trying to force Vim to follow the same model through its configuration.

I think a good way to understand the differences in concept and usage between Vim’s three levels of view abstraction — buffers, windows, and tabs — is to learn how to use each from the ground up. So let’s start with buffers.

Buffers

A buffer in Vim is probably best thought of for most users as an open instance of a file, that may not necessarily be visible on the current screen. There’s probably a more accurate technical definition as a buffer need not actually correspond to a file on your disk, but it’ll work for our purposes.

When you open a single file in Vim, that file gets put into a buffer, and it’s the only buffer there on startup. If this buffer is unmodified (or if you’re using hidden), and you open another file with :edit, that buffer goes into the background and you start working with the new file. The previous buffer only goes away when you explicitly delete it with a call to :quit or :bdelete, and even then it’s still recoverable unless you do :bwipe.

By default, a buffer can only be in the background like this if it’s unmodified. You can remove this restriction if you like with the :set hidden option.

You can get a quick list of the buffers open in a Vim session with :ls. This all means that when most people think of tabs in more familiar text editors, the idea of a buffer in Vim is actually closest to what they’re thinking.

Windows

A window in Vim is a viewport onto a single buffer. When you open a new window with :split or :vsplit, you can include a filename in the call. This opens the file in a new buffer, and then opens a new window as a view onto it. A buffer can be viewed in multiple windows within a single Vim session; you could have a hundred windows editing the same buffer if you wanted to.

Windows in Vim

Vim Session with multiple windows open, both horizontally and vertically.

Tabs

Finally, a tab in Vim is a collection of one or more windows. The best way to think of tabs, therefore, is as enabling you to group windows usefully. For example, if you were working on code for both a client program and a server program, you could have the files for the client program open in the first tab, perhaps in three or four windows, and the files for the server program open in another, which could be, say, seven windows. You can then flick back and forth between the tabs using :tabn and :tabp.

Tab usage in Vim

Two tabs open in Vim, each with multiple windows. Move between two tabs with :tabn and :tabp.

This model of buffers, windows, and tabs is quite a bit more intricate than in other editors, and the terms are sometimes confusing. However, if you know a bit about how they’re supposed to work, you get a great deal more control over the layout of your editing sessions as a result. Personally, I find I rarely need tabs as with a monitor of sufficient size it suffices to keep two or three windows open at a time when working on sets of files, but on the odd occasion I do need tabs, I do find them tremendously useful.

Thanks to Hacker News users anonymous and m0shen for pointing out potential confusion with the :set hidden option and the :bdelete command.

33 thoughts on “Buffers, windows, and tabs

  1. Nice writeup, I use tabs all the time and them very useful for inspecting multiple files. Don’t forget gt and gT for switching forward and back respectively, much faster than :tabn and :tabp .

  2. Hi, as many direct shortcuts exists as alternative way to switch bufffers or window like C-w C-v to split vertically, changing tabs with :tabn isn’t really acceptable. I suggest adding that typing gt and gT to go back and forth is simply better :D

  3. I don’t know where does the chance come from I need use tabs, windows and buffers always meet my requirement.

    • Funny, for me it’s the other way round. I always use tabs and never windows/buffers. Somehow i dislike that buffers aren’t always visible.

  4. Thanks for the write up but what do you recommend for working within and controlling the display of windows on a tab?

    • This was brought up on the Hacker News thread about this post. I intend to write a followup with more practical advice about managing buffers, windows, and tabs in Vim.

      In the meantime, you could always read :help window.

  5. Pingback: VIM: Buffers, windows, and tabs | look!

  6. Great article, thanks! Sometimes if I explicitly want to edit a list of files, I use something like “vim -p *.conf” to open all files in separate tabs to see it _visually_ without checking the buffers with :ls

  7. Pingback: Hardcore VIM’ing für die härtesten der harten ;) | ~~~feverM!ND'zZ'dReAm~~~

    • I would say it is better to not involve Tmux in your window management of Vim. If you’re running seperate processes of Vim you can’t yank and paste across them and so forth.

  8. I have the following in my .vimrc file to make working with tabs easier:

    map + :tabnew
    map <C-Tab> :tabn
    map <S-Tab> :tabp
    

    The plus sign creates a new tab, CTRL+TAB navigates to the next tab and SHIFT+TAB moves to the previous tab.

    It’s still important to understand buffers and what is happening “behind the scenes” …

  9. I always use tabs and move with ctrl-pgup and ctrl-pgdn between them. In fact, I have this in my .bashrc:

    alias vim="vim -p"
    less(){
        if [ $# -ne 0 ]; then vim "+noremap q :q" "+noremap  " -p -M -R "$@"
        else vim "+noremap q :q" "+noremap  " -p -M -R -
        fi
    }
    

    and this in my .vimrc:

    cabbrev tabv tabnew\|setlocal nomodifiable\|view
    

    I find these useful with tabs.

  10. Pingback: Lazy Reading for 2012/06/17 – DragonFly BSD Digest

  11. "Tab navigation 
    nmap sn :tabnew
    nmap sc :tabclose
    nmap sl :tabnext
    nmap sj :tabfirst
    nmap sh :tabprevious
    nmap sk :tablast
    nmap st :tabs
    
    "Control-T for new tab
    nmap  :tabnew
    
    "Alt # to switch
    map 1 1gt
    map 2 2gt
    map 3 3gt
    map 4 4gt
    map 5 5gt
    map 6 6gt
    map 7 7gt
    map 8 8gt
    map 9 9gt
    
  12. Pingback: Buffers, windows, and tabs | Arabesque | zzzzzzzzz

  13. Thanks for keeping the article so simple even a newbie could understand it :) Great way of getting introduced to Vim’s (sometimes mystic like) way of thinking.

  14. I’m in the process of making the switch to Vim. This post was my “aha” moment. Nice job breaking down the basics of how buffers, tabs and windows work.

  15. Nice article. I previously used tabs but once I understood how to (efficiently) use buffers it felt much more natural. Another good article on buffers/windows is How I learned to stop worrying and love Vim buffers.

    I believe one of the reasons maybe most people prefer (or atleast use) tabs over buffers is simply because buffers are not visual. When you’re using to seeing your open files using buffers might not feel right. Using a plugin such as bufferline, minibufexpl or similar makes everything much easier.

    You can even integrate bufferline with your statusline if you’re using lightline or airline. Take for example my configuration as an example:

    github.com/timss -> vimconf -> lightline/bufferline configuration

  16. Pingback: Vim | Pearltrees

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use Markdown if you want.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>