Vim tutorial-2: Tab and Window, Buffer and File
Apr. 12, 2021
I will introduce the concept of tab and window, buffer and file, and their relationships.
Buffer, file, window, tab are the most basic things in an editor, but they are easily ignored by many users. Fig. 1 summarizes the relationships among tabs and windows, buffers and files in Vim.
File is the easiest concept to understand here, which is the file on the disk. The core task of an editor is to read the content of a file, edit it and save it back to the disk. A basic computer hardware knowledge is that the read and write to a disk is super-slow compared to the main memory. So most of the software will create a temporary object in the main memory and write the content to the disk when the job is done. In Vim, buffer is the that object.
:edit filename
;filename
. Then, Vim will see if
filename
exists. If it exists, Vim will load the content in the disk to
the buffer and you can edit it. Otherwise, Vim will do nothing, and you can
edit an empty buffer.:write
to write to the disk. If the file
already exists on the disk, Vim will replace the content with the new one.
Otherwise, Vim will create a new file on the disk and write the content to
it.There are something you need to be careful.
:write
or :w
. So the two commands that Vim used to
interact with file on the disk is :write
and :edit
, while all other
editing commands is for the user to interact with the buffer.When you enter Vim though vim
command, you will have one window in one tab.
Then you can use :split
to split the window horizontally (:vsplit
for
vertical splitting), and you will have several windows (like
Fig. 1) in one tab.
To create new tabs, you can use :tabnew
to create more tabs, and you can split
windows in the new tabs.
To jump to other window, the most basic key-mappings are <Ctrl-w>h
,
<Ctrl-w>j
, <Ctrl-w>k
and <Ctrl-w>l
. It will jump to the adjacent window
relative to the current cursor position. For example, if you press <Ctrl-w>l
in Win-1
in Fig. 1, which window it will jump to? If your
cursor is at the upper half of Win-1
, it will jump to Win-2
. Otherwise, it
will jump to Win-3
.
If there are too many windows, a useful plugin is vim-choosewin. It will generate alphabetic characters for each window, and you can jump to the target window by pressing the corresponding character. Another useful function of this plugin is that you can swap the buffers of two windows.
Sometimes, you might want to maximize one window. For example, you opened the documentation in a small splitting window, which is inconvenient for reading, so you want to maximize it. After that, you want to restore to the previous window layout. There are many plugins out there for this, and I found a stable one, vim-maximizer.
To navigate the tabs, you can use :tabnext
and :tabprevious
, and I mapping
these two commands to ]t
and [t
.
To customize the name of the tab, you can use the plugin taboo.vim.
A conceptual knowledge of the relation between buffer and file is sufficient. The tab and window are more practical, and you need to use them to maximize the usage of your screen.