vim: Part 1: Buffers, Windows, Tabs

Any developer involved in a largish 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:

  • being able to move from one active file to another seamlessly
  • active viewing area

vim provides multiples ways to do it.

Before we get to details let’s understand the terms buffers, windows, and tabs.

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.

window is a viewport on a buffer whereas

tab is a collection of windows

Lets looks at some commands related to buffers

Open a text file in VIM

vim lorem.txt

In VIM do a  :ls to view all buffers you can open an alternate (indicated by #) buffer by giving a :edit cal.txt .  View all buffers again and you will see

1 # "lorem.txt" line 1
2 %a "cal.txt" line 1

1, 2 above are buffer numbers and the ‘line’ number indicate current position in each buffer.

The ‘#’ indicates that this is an alternate buffer and the current buffer indicated by ‘%’ is ‘cal.txt’ and is active indicated by ‘a’

easiest way to switch from one buffer to another is by issuing :bn command where ‘n’ is the buffer number

Go ahead and try to to switch from one buffer to another for editing. Modify a line in any buffer and check the list of buffers – you will notice a ‘+’ in the status to indicate that this buffer has been modified.

1 # "lorem.txt" line 1
2 %a + "cal.txt" line 1

If you vim file1 file2 file3 VIM will create 3 buffers the first one will be the active one. Using :ls lists all buffers

1 %a "file1"      line 1
2    "file2"      line 0
3    "file3"      line 0
If you want to work with windows instead of buffers then here is a quick way to convert all buffers to windows

Go ahead and try this.
We will explore windows in Part 2