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
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
Escfrom 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
vfrom Normal mode;
Shift+vselects lines and
Ctrl+vselects text blocks.
- Insert (
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
i; Replace mode is accessed with
- 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 (
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.
So every file you open will be placed in a Buffer, that can be displayed or hidden. You can cycle through buffers with the
:bprev commands and list them all with the
:lslist all buffers
:buffer [N]show the buffer with the provided number
:bprevshow 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
: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).
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.
:split [filename]split the window, optionally opening a new file
:vsplit [filename]split the window vertically, optionally opening a new file
:sbuffer [N]split the window and open the buffer [N]
:sballopen all buffers in windows
Ctrl-w Wgo to next/prev window
Ctrl-w [direction]go to the window at [direction]
:qclose a window (if it is the last window it also exits the editor)
:qaclose all windows and exit the editor
Many famous Vim plugins like NERDTree use Windows to show lists or the filesystem.
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
:tabprevshow next/previous tab
:tab sballopen all buffers in tabs
: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 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
: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" page.