Vim Buffers, Windows and Tabs — an overview
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 with. But when you need to open several files it is necessary to know at least a minimum about Buffers, Windows, and Tabs and I intend to give 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 open 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, 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 select lines and Ctrl+v select text blocks.
- Insert (a, i) and Replace (R) and 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 : key from Normal mode.
Most of the key mappings shown bellow 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 they are all opened but you’ll only be seeing the first. That is because vim open 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 not. You can cycle through buffers with :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, keeping it in memory, thus not removing from the buffer list.
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 uses 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, much like tabs in a browser.
- :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
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 showing each one 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, :bnext and :bprev from start and just use more (split) windows or tabs as needed.