Any developer involved in a large software developement project is actively editing multiple source files. There are multiple factors that makes such a developer efficient in the task of editing multiple files. Generally speaking such efficiency depends on:
- navigating from one active file to another seamlessly
- and active viewing area
vim provides way to do this using buffers, windows and tabs. In this article we will focus on buffers and windows
Before we get to the details lets understand the terms buffers and windows in vim.
- A buffer is the in-memory text of the file that was opened using vim. It may be visible or hidden. It is identified by a buffer number and has flags to indicate whether it is current, has been modified or not etc.
- A window is a view port on a buffer whereas
- A tab is a collection of windows
When vim opens a file, a buffer is associated with it.
vi server.js
Not every buffer is associated with a file. We will see an example later.
Once in vim. Enter the following command in normal mode
:ls
which will show buffer information as below:
1 %a "server.js" line 1
Press ENTER or type command to continue
- The integer
1
refers to the buffer number - The
%a
is a flag indicating that this is the active buffer -
"server.js"
the filename and -
line 1
indicate the current line on which the cursor is positioned
Lets now open two more buffers. While in vim you can open another file by:
:edit config.json
Lets also create a buffer without any file associated with it:
:enew
and list all the buffers using :ls
vim command.
1 "server.js" line 1
2 # "config.json" line 1
3 %a "[No Name]" line 1
Press ENTER or type command to continue
Note: buffer number 3 has "no name" and is active.
Switching between buffers is easy. Use :bn
where n
is buffer number :b2
will switch to buffer 2, makin it active.
Switching between buffers will work if the changes to a buffer is saved.
To view all buffers enter the following command in vim
:ball
You will now see three horizonal windows. You can switch from one to another by followed by any of the arrow keys.
Try :ls
to check all buffers are now active as they are all visible.
You can close the the current window by followed by 'c' or by entering :q
command. The window closes but the buffer is still there.
for more information on vim buffers enter the following command in vim
:help buffers
Next week look out for more on windows and tabs in Part-2 of this post.
Top comments (0)