Vim Buffers, Windows and Tabs  – An Overview

I have been using Vim since I started using Linux, many years ago, but it was just recently that I decided to use it as my main text editor. There are a lot of reasons for that: from being fast, to very productive, to highly customizable, to be available in almost any Linux or Mac installation. But there is a (small) learning curve for anyone who wants to start using Vim, and I’m not speaking of hitting :q to exit the editor.

For an occasional user, it is enough to know that you hit a, i or Insert to start writing, Esc when you finish and :wq to write and exit Vim. But when you need to open several files it is necessary to know at least the minimum about Buffers, Windows, and Tabs, and I intend to give a quick explanation about these.

Before we start, let’s talk about vim modes…

Vim has several modes that act very differently and, thus, have different key mappings. They are Normal, Visual, Insert, Replace, and Command.

  • Normal (Esc) is the default mode, the one it starts when opening Vim in a terminal (or Gvim, if you’re using the GUI version). In this mode, each key can have a different mapping, including the letter keys. You cannot write text, but you can cut and paste. You can always return to Normal mode by pressing Esc from any other Vim mode.
  • Visual (v) is much like Normal mode, but it is intended mainly to select blocks or lines of text for copying or other actions. Visual mode is accessed with v from Normal mode; Shift+v selects lines and Ctrl+v selects text blocks.
  • Insert (a, i) and Replace (R) are both text editing modes, with the only difference being that the latter replaces the text under the cursor. Most of the key mappings that work in Normal and Visual mode won’t work in Insert or Replace. Insert mode is accessed with a, i; Replace mode is accessed with R.
  • Command (:) is the mode used to write and execute commands, and you already know it. Command mode is accessed with the : key, from Normal mode.

Most of the key mappings shown below to cycle through Buffers, Windows and Tabs should be used in Normal Mode.

Where are the files I opened?

Vim can open and edit several files at once. So if you write in a terminal vim file1.txt file2.txt file3.txt, all three files will be opened, but you’ll only be seeing the first. That is because Vim opens files in Buffers.

From Vim help (:h buffers):

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.

Buffers

So every file you open will be placed in a Buffer, that can be displayed or hidden. You can cycle through buffers with the :bnext and :bprev commands and list them all with the :buffers command.

  • :buffers:ls list all buffers
  • :buffer [N] show the buffer with the provided number
  • :bnext/:bprev show next/previous buffer
  • :edit [filename] put a new file into a buffer
  • :bdelete [N] delete the current buffer or the buffer [N]
  • [N] Ctrl-^ go to buffer [N] or the previously shown buffer

Note that :q doesn’t delete a buffer; the buffer is kept in memory and not removed from the buffer list. Instead, :q is used to close a Window or Tab (see below).

Windows

A Vim Window is what you see and interact with. A Window always shows a Buffer, being it from a file or just an empty one. Many windows can be opened at the same time by splitting the editor.

  • Ctrl-w s:split [filename] split the window, optionally opening a new file
  • Ctrl-w v:vsplit [filename] split the window vertically, optionally opening a new file
  • :sbuffer [N] split the window and open the buffer [N]
  • :sball open all buffers in windows
  • Ctrl-w w/Ctrl-w W go to next/prev window
  • Ctrl-w [direction] go to the window at [direction]
  • :q close a window (if it is the last window it also exits the editor)
  • :qa close all windows and exit the editor

Many famous Vim plugins like NERDTree use Windows to show lists or the filesystem.

Tabs

A tab can show one or more windows, and if more than one tab exists, a list of tabs is shown at the top of the editor, looking like tabs in a browser. But tabs work in a different way in Vim, they are more like viewports or workspaces; their main function is to group Windows together.

  • :tabnew [filename] create a new tab, optionally opening a file
  • gt/gT , :tabnext/:tabprev show next/previous tab
  • :tab sball open all buffers in tabs

The :q command will also close the tab if there is only a single window visible in it.

Putting all together

To summarize, a Window can show the contents of a buffer, and a Tab can host one or more Windows.

By using many Vim Tabs one can almost reproduce the look of a graphical text editor (think of Atom or VS Code), with several tabs, each showing a file. They are easy to look at or navigate with gt/gT and can also be moved around. But after some time this choice becomes a bit limited because it is much faster to cycle through buffers once you are used to them.

A good recommendation is to get used to buffer commands like :ls, :buffer:bnext and :bprev from start and just use more (split) windows or tabs as needed.


This post was first publised on medium.com/@paulodiovani/vim-buffers-windows-and-tabs-an-overview-8e2a57c57afa.

We want to work with you. Check out our "What We Do" section!