A Few Good Things about the Tab Page Feature in Vim

0
5275
Tab feature page visual
The inclusion of the tab page feature in Vim has enabled consolidation of several sessions in one big window, and has made movement from one file to another very simple.

Almost every graphical text editor provides a tab page feature. At first sight, it appears that Vim may have missed out on this hefty feature, but that’s not true. Vim has great support for tab pages. It even allows the opening of multiple files under the same tab page via a window-splitting feature. The tab page feature was introduced in Vim in version 7.0 (released in May 2006). It is considered one of the best features of Vim. This column explores the tab page feature of the Vim editor. It is assumed that readers have some basic understanding of Vim.
Before delving deep into tab page features, let us understand how tabs are different from buffers and windows. Files store data on the disk in a persistent manner, whereas buffers reside in the memory. A buffer is an in-memory representation of a file. When we open any file in Vim, it is loaded into the buffer, and even when we perform any changes, we are actually modifying the buffer. For instance, the command below loads contents of the two files in separate buffers.

[bash]$ vim file1.txt file2.txt

We can easily switch to the next buffer by executing the :bnext command.
A window is a view-port into a buffer. Vim allows the viewing of multiple buffers side by side by dividing the workspace into multiple windows. Each window can open the same or a different buffer. Windows can be split either horizontally or vertically. A tab page is just a collection of one or more windows placed side by side. We can also open multiple windows inside each tab page. Shortly, we will look at an example of the same.

Working with tab pages
Now that we have learned theoretical fundamentals of tab pages, it’s time to do something practical. We can instruct Vim to open tab pages in multiple ways. One of the easiest is to open tab pages at start-up. The syntax of Vim’s command-line argument to open multiple tab pages is given below.

vim -p[N]

In the above syntax, ‘N’ implies the number of tab pages and square bracket implies it’s an optional parameter. To open three tab pages at start-up, use the command shown below.

[bash]$ vim -p3
Figure 1
Figure 1 : Tab pages 

Please note that -p3 is a single word. This command opens three empty tab pages as shown in Figure 1.
Alternatively, we can also provide files to open them in separate tab pages. For instance, the command below instructs Vim to open each file in a new tab page.

[bash]$ vim -p file1.txt file2.txt

If the number of tab pages is greater than the number of files provided at the command line, then the remaining tab pages open empty buffers. For instance, the command below opens a total of four tab pages, where the first two show file1.txt and file2.txt, respectively, while the remaining two show empty buffers.

[bash] vim -p4 file1.txt file2.txt

What if we want to open a tab page in the middle of our work? Don’t worry about it. There is no need to close the current Vim session. Vim developers have already thought about this. There are two more commands, using which we can open tab pages in a running instance of Vim. We can use either of the following three commands— :tabnew, :tabedit or :tabe. The syntax for each command is given below:

:[N]tabnew [filename]
:[N]tabe [filename]
:[N]tabedit [filename]

If you omit the file name with the above commands then Vim opens empty tab pages; otherwise, it loads the file into a new tab page. If ‘N’ is omitted, the new tab page appears after the current one; otherwise, Vim opens a new tab page after the Nth tab page.
Tab pages are really awesome. But the question is: how many tab pages can we open? By default, we can open up to 10 tab pages, but this behaviour can be controlled by setting the tabpagemax option. For instance, to set an upper cap of 20 tab pages, add the following line into ~/.vimrc:

set tabpagemax=20

If the number of tabs allowed by tabpagemax is exceeded, Vim will simply open the maximum number of tabs, and the other files will be opened but not displayed. You can edit the remaining files by using the :next or :last commands to move to the files that are not displayed in the tab pages. Please note that this setting is applicable only for the ‘-p’ option; you can open as many tab pages as you like from a running instance of Vim.
In addition to this, Vim also provides the tabfind or tabf command, which finds the file in the current path and opens it in a new tab page. It uses Vim’s path option to determine which directories should be searched when opening the specified file. To determine the value of the current path, just execute the :set path command. On my system, its value is as follows:

path=.,/usr/include,,

The above path instructs Vim to look in the directory containing the current file (period(.)), then the /usr/include directory and, finally, the current directory (the empty text between two commas). To append an additional search path, execute the following command:

:set path += /etc

The steps below demonstrate how to use the tabfind command to find and open files in a new tab page:

[bash]$ cd /tmp/test # Deliberately switch to the empty directory.

[bash]$ ls # Verify it is empty.

[bash]$ vim # Open instance of vim and execute the command below from vim
:tabfind malloc.h

Though the /tmp/test directory doesn’t contain the malloc.h file, Vim opens it by searching for it in the /usr/include path. You can also use regular expressions with tabfind. Just as in the :tabedit command, when ‘N’ is omitted with the :tabfind command, a new tab page appears after the current one; otherwise, Vim opens a new tab page after the Nth tab page.
So far, we have seen several ways to open tab pages. Now, let us quickly go through the steps to close the tab pages. To close tab pages, we can use any of the following commands according to our requirements:

:tabclose
:tabclose [N]
:tabonly

As the name suggests, the :tabclose command closes the current tab page. But this command may fail, if the current tab page is the last tab page or the in-memory buffer is modified but not yet written to the disk.
The :tabclose [N] command closes the Nth tab page. This command can fail in the same way as the :tabclose command. Please note that the tab page counting begins from ‘1′. For instance, to close the third tab page, use the syntax below:

:tabclose 3

The :tabonly command closes all other tab pages and shows the current tab page only. This command execution can fail if the in-memory buffer of any tab page is modified but not yet written to the disk. In addition to this, to close the current tab, we can use Vim’s regular commands as well—for instance, :wq, :q!, :x, ZZ and so on.

tabs
Figure 2 : Tabs

Navigating tab pages
Vim provides several ways to get information about tab pages. For instance, the :tabs command provides a summary of all open tabs. It shows a > symbol for the current window and shows a + symbol for modified buffers. Figure 2 illustrates this.
By default, tab page labels are shown at the top of the Vim window only when tab pages are open. To display tab page labels all the time, use the following command:

:set showtabline=2

Similarly, to hide the tab page labels, use 0 instead of 2 in the above command.
We can use any of the commands below to navigate tab pages:

:tabn
:tabn [N]
:tabnext
:tabnext [N]
:tabN
:tabN [N]
:tabNext
:tabNext [N]
:tabp
:tabp [N]
:tabprevious
:tabprevious [N]

As the name suggests, the :tabn command jumps to the next tab page. Tab page navigation wraps around from the last to the first one. For instance, if there are five tab pages open and currently we are at the fifth tab page, then execution of the :tabn command causes it to go to the first tab page. Similarly, :tabn [N] jumps to the Nth tab page. :tabnext and :tabnext [N] work exactly like the :tabn and :tabn [N] commands, respectively.

The :tabN command jumps to the previous tab page. Tab page navigation wraps around from the first one to the last one. For instance, if there are five tab pages open and currently we are at the first tab page, then execution of the :tabN command causes it to go to the fifth tab page. Similarly, :tabN [N] goes N tab pages back. :tabNext and :tabNext [N] work exactly similar to the :tabN and :tabN [N] commands, respectively.
The :tabp and :tabprevious commands work exactly the same way as the :tabN command. The :tabprevious [N] and :tabp [N] commands work the same way as the :tabN [N] command.
In addition to this, we can directly jump to the first or last tab page by using the following commands.
The :tabfirst or :tabrewind command jumps to the first tab page. Similarly, the :tablast command jumps to the last tab page. Vim also allows tab page navigation in command and insert mode. For instance, in command mode, we can use the following commands.
gt : Jumps to the next tab page. Tab page navigation wraps around from the last to the first one.
gT : Jumps to the previous tab page. Tab page navigation wraps around from the first one to the last one.
[N]gt : Jumps to the ‘N’th tab page in forward direction. Tab page navigation wraps around from the last to the first one.
[N]gT: Jumps to the ‘N’th tab page in backward direction. Tab page navigation wraps around from the first one to the last one.
In addition to the above commands, we can use the following tab page navigation commands either in command or insert mode.
Ctrl + PageDown : Jumps to the next tab page. Tab page navigation wraps around from the last to the first one.
Ctrl + PageUp: Jumps to the previous tab page. Tab page navigation wraps around from the first one to the last one.
[N]Ctrl + PageDown: Jumps to the ‘N’th tab page in forward direction. Tab page navigation wraps around from the last to the first one.
[N]Ctrl + PageUp: Jumps to the ‘N’th tab page in backward direction. Tab page navigation wraps around from the first one to the last one.
Miscellaneous commands
We have seen that we can open and navigate tab pages in any order. Wouldn’t it be great if we could rearrange the order of the tab pages according to our choice? This can certainly be done by using the following set of commands:

:tabmove
:tabmove 0
:tabmove [N]
:tabm
:tabm 0
:tabm [N]

The :tabmov command moves the current tab page to the last position. To move the current tab page to the first position, execute the following command:

:tabmove 0

Similarly, the :tabmove [N] command moves the current tab page to the N + 1th position. Please note that the :tabmove command counts tab pages from zero. Hence, :tabmove 4 moves the current tab page to the fifth position. :tabm is just the abbreviated form of the :tabmove command.
The :tabdo or :tabd commands are extremely useful when we want to execute certain commands in each tab page. Here is the syntax of the command:

:tabdo <command>
:tabd <command>

Here, the angular bracket implies the command is mandatory. Listed below is the workflow of the :tabdo command.
Step 1: Go to the first tab page.
Step 2: Execute the specified command in the current tab page.
Step 3: If it is a last tab page then stop the execution of the command.
Step 4: Otherwise jump to the next tab page and go to Step 2.
Let us suppose that we have opened multiple tab pages and want to replace the word ‘Tom’ with ‘Jerry’ on each tab page; then the following command will do the needful:

:tabdo %s/\<Tom\>/Jerry

Please note that one of the limitations of this command is that it can only execute the same command in each tab page.
Vim already supports the command that works nicely with split windows. Vim provides the :tab command line modifier to use a new tab page instead of a new window for commands that would normally split a window. For instance, the :tab ball command shows each buffer in a separate tab page. The simple example given below illustrates the same.

[bash]$ vim file1.txt file2.txt file3.txt

In the above command, Vim opens three files in separate buffers but only shows a single buffer/ window at a time. We can use the :bnext command to switch to the next buffer. Now, to open these buffers into separate tab pages, execute the :tab ball command from the current Vim session.
Additionally, we can view Vim’s help contents in a new tab page. By default, the :help command splits the window horizontally and displays help contents in the split window. To view help contents in a separate tab page, execute the following command from the Vim session, as shown below:

:tab help

Similarly, the :tab split [filename] command opens a file in a new tab page rather than splitting he window. If the filename is omitted with this command, it copies the current window to a new tab page. Otherwise, it opens the specified file in a new tab page. For instance, the command below opens the file1.txt file in a new tab page.

[bash]$ vim file1.txt # Now execute the command below from Vim
:tab split

Wouldn’t it be great if we could use both tab pages and split window features together? Vim provides that flexibility. Let us suppose you have opened several files in a split window and you want to move a certain file to a new tab page. Just press Ctrl+W T. Similarly, to close the current tab page, use the Ctrl+W c key combination.
We can also use the goto file feature with the tab pages. By using this, we can put the cursor on the file name and open the file into a new tab page by pressing the Ctrl+W gf key combination. The example below illustrates this feature:

[bash]$ echo "This is file#1" > file1.txt

[bash]$ echo "file1.txt" > file2.txt

[bash]$ vim file2.txt # Now execute the command below from Vim
Ctrl+W gf or
Ctrl+W gF

This will open file1.txt in a new tab page. Please note that you must set the appropriate ‘path’ option to use this feature.
Myriad tasks can be done with Vim. Only a few keystrokes are required to solve complex tasks. These simple yet powerful features really make Vim more interesting. To gain insights into tab pages, go through Vim’s help documentation. You can access the document by executing the :help tab-page-intro command from the Vim session.

LEAVE A REPLY

Please enter your comment!
Please enter your name here