Vim tips: use tabs

Before Vim 7.0 was released last May, every time I edited the file, I had to open up to 5, 6 xterm windows or Konsole. Each window is a separate Session Vim with a single file to edit. This makes me take up too much space on the screen and is very bad

Before Vim 7.0 was released last May, every time I edited the file, I had to open up to 5, 6 xterm windows or Konsole. Each window is a separate Session Vim with a single file to edit. This made me lose too much space on the screen and was very inefficient. Vim 7.0 allows users to choose to use tabs, unify all sessions into one window and move between files easily.

If you are using Vim version before 7.0, you do not have access to this component. Now most distros switch to Vim 7.0, so you must use the latest version to use them.

Open a tab

Let's start with opening a new tab in Vim. There are several ways to do this. The easiest to remember is to run : tabnew in normal mode. This command will open a new tab with an empty buffer. If you want to edit the file in a new tab, you can run the command : tabnew filename . Vim will load the file in the new tab for you.

Picture 1 of Vim tips: use tabs
Another way is to open more than one file at startup, using the -p option. If you want to open three files in separate tabs, use the following syntax:

vim -p file1 file2 file3

This command will start a Vim session (Vim session) with file1 in the first tab, file2 in the second tab and file3 in the third tab.

Vim will open tabs according to the number you require on starup. The largest number of tabs is set in the vimrc file. The default maximum value is 10, but you can change it by setting your tabpagemax option in .vimrc , as follows:

set tabpagemax = 15

If you exceed the number in tabpagemax , Vim will open the maximum number of tabs allowed, while the other files will still be open but not displayed. You can edit the remaining files by using the : netxt or last command to move files that are not displayed in a tab. Note that this setting only applies to the largest number of tabs Vim will open on starup. You can still open more tabs during Vim's operation.

The command : tabf allows you to search the file in the current path and open it in a new tab. For example, if you want to open a file named inventory.txt in the current path, you can run:

: tabf inven *

This command will find the file that matches the inven string and any numeric characters after it. If only one file is found, Vim will open it in the new tab. If there are several suitable files, Vim will notify you of too many files and you must limit the search process to only one file. Command : tabn will automatically complete the file name in your path. So you just type the first few characters of filename and press Tab to find the right file name.

Move between tabs

Picture 2 of Vim tips: use tabs
You can change between tabs using the command : tabn and : tabp or gt if you're in normal mode. Of course, if you're using Vim's GUI, GVIM, you can use the mouse or shortcut to switch between tabs.

In GVim, you can access a context menu of tabs by right-clicking on the tab bar. You can then open a new tab with a new buffer, an existing file or close the current tab.

If you're opening multiple tabs, you can use : tabfirst or briefly : tabfir to jump to the first tab and : tablast to jump to the last tab.

By default, tab labels are only displayed at the top of the Vim window when tabs are open. If you want to see the title bar of the tab continuously, you can edit the showtabline option in the .vimrc folder. To set this option, use:

set showtabline = 2

If you want to turn it off completely, use 0 or 2.

Notice that the tabs are still there, even though the bar tab is not displayed. If the tabline option is set to 0, you can know which tab is open by using the : tabs command. It will provide a summary of open tabs, as you can see in the illustration.

Talking about setting options, if you don't like the existing shortcuts, you can add your own shortcut. For example, if you want to open a new tab easily, you can insert the following command into .vimrc :

imap, t: tabnew

This command tells Vim to set up keyboard shortcuts, t in insert mode. To run Esc , place Vim in normal mode, so that : tabnew and carriage (CR) return to run the command. You can set shortcut maps for the entire command to manipulate the most frequently used tabs.

Rearrange tabs

Be careful, you'll want to arrange tabs in neat and neat Vim. You can move them to a specific place in a certain order by using the command : tabm n , where n is the number of locations you want. If no arguments for the : tabm command are provided , the current tab will be moved to the last position.

Vim starts the tab number from 0. So, if you open 6 tabs, you will have tabs from 0 to 5. If you're on the first tab and want to go to the fourth tab, you can use the command :: tab 3 .

Note that you can still use viewports as usual in the tab window. Using a tab is useful if you want to quickly edit a file, especially when the Vim window itself sets up two views.

Run the commands in the tab

Suppose you are editing 6 or 7 files in Vim and realize that you need to replace a variable name. Using : tabdo , you can execute a search command and replace all tabs at once instead of having to change each file individually. For example, if you want to replace foo for a bar, run the following command:

: tabdo% s / foo / bar / g

This command will run through each open tab and perform a search, replacing (% s / foo / bar / g) in each of those tabs.

The tabs will become extremely useful while just spending a little time to master it. If you want more information when working with tabs, you can run the command : help tab-page-intro inside Vim, the help will appear.

Update 14 December 2018
« PREV
NEXT »
Category

Technology

Life

Discover science

Medicine - Health

Event

Entertainment