[Zz] Vim tab Function

Source: Internet
Author: User
ArticleDirectory
    • 1. Create a new tab
    • 2. Remote-tab function of gvim
    • 3. jump between tabs
    • 4. Tab sorting
    • 5. Optimize the display of tab names
    • 6 tabs and sessions

Although I have been using Vim to writeProgramAnd documentation, but has not gone into the specific use of its tab page function. Now I am paying attention to this issue. After all, it takes less and less time to use Vim. Of course, this is only for all my life.

1. Create a new tab

Use Vim to create or open a file in the current working directory and open a new tab to display the file content. The operations are as follows:

: Tabnew filename

Use the tabf command to recursively traverse the current working directory based on the regular expression to find the file with the matched name and create a new tab for it. The usage is as follows:

: Tabf filename_re

The premise of the above operation is to locate the vim working directory to the desired location, otherwise the modified file may be stored in a directory you do not know. When you use Vim in a terminal to open a file, the current directory is used as the vim working directory by default, but when you use gvim, if you open a text file directly from the File Manager provided by the desktop environment, gvim's working directory is $ home by default. Like me, you may like to automatically migrate the vim working directory to the directory of the opened text file. The solution is to edit the $ home/. vimrc file and add the followingCode:

"Change work dir to current dir
Autocmd bufenter * cd %: P: H2 gvim remote-tab Function

Gvim provides the remote-tab function, which allows you to create a tab while opening a file in gvim. Here, the concept of "remote" means that gvim is like a currently running server program. When you use commands such as "gvim -- remote-tab-silent FILENAME" in a terminal, the "Open Document and create tab for it" command is passed to the currently running gvim process, and the gvim process will execute this command. The advantage of this function is that you only need to open a gvim process. All files to be edited are displayed in the gvim process as tabs, this facilitates the editing and Management of Multi-Point files belonging to a specific project.

First, turn off all the gvim processes you are running, and then execute the following commands in sequence to experience the functions of Remote-tab.

$ Gvim a.txt
$ Gvim -- remote-tab-silent B .txt
$ Gvim -- remote-tab-silent c.txt d.txt e.txt

In practical applications, you can use the alias function provided by Shell to simplify long command lines such as "gvim -- remote-tab-silent" by editing $ home /. add the following to the bashrc file:

Alias Vi = 'gvim -- remote-tab-silent'

What alias is used depends on your preferences. The reason why I use "Vi" as the alias of the "gvim -- remote-tab-silent" command is that I do not frequently use the VI command, and in many Linux releases, "Vi" is connected to "Vim" by default.

Now we use the VI alias to implement the previous example:

$ VI a.txt B .txt c.txt d.txt e.txt

Previously, I used to use "Vim" to enable Vim on the terminal and use "gvim" to enable Vim GUI, but not the "Vi" command, however, from now on, I may use the most VI commands.

3. jump between tabs

You can use ": TABP" and ": tabn" to switch the current tab in a group of tabs forward or backward, but this is too cumbersome, to switch the current tab to an adjacent tab, You need to press the key seven times. By using the key ing function provided by VIM, You can map the ": TABP" and ": tabn" commands to your favorite shortcut keys. I am keen to map ": TABP" to "Shift + Left", and map ": tabn" to "Shift + Right", by editing $ home /. vimrc, add:

"Shift tab pages
Map <s-left>: TABP <CR>
Map <s-Right>: Sorting of tabn <CR> 4 tabs

When multiple tabs are opened, the order of the tabs in the tab bar is not what we want. You need to adjust them manually. Many editors support the use of mouse drag and drop to adjust the order of tabs, but Vim is not. Currently, you can only adjust the order of tabs using the tab number mechanism and command mode.

In the tab column, each tab is numbered, starting from 0 and increasing from left to right by 1.

You can use the ': tabm n' command to change the number of the current tag to 'n', which is used to adjust the position of the current tab to the position of the corresponding Tab Of the number 'n.

5. Optimize the display of tab names

In the label bar of VIM, the names of tabs are abbreviated to the full name of the corresponding file path by default, for example, the tab name corresponding to the/usr/share/doc/test.txt file is/u/S/D/test.txt by default. Such a tab name looks a bit strange. I don't think it is necessary to display the path name of the Tag file, because in most cases, the user is very clear about the location of the edited file, even if you forget it for a moment, you can also use ': f' command to view the file name. Especially in gvim, When you hover the mouse over the tab name, a very small Prompt window appears, showing the full name of the label file path.

Below is my hack for the vim tab. In the tab bar, the path information of the currently edited file is removed and only the file name is retained.

Function shorttablabel ()
Let bufnrlist = tabpagebuflist (V: lnum)
Let label = bufname (bufnrlist [tabpagewinnr (V: lnum)-1])
Let filename = fnamemodify (Label, ': t ')
Return filename
Endfunction

Set guitablabel =%{ shorttablabel ()}

6 tabs and sessions

Vim can store the current working status as a session file. by reading the session file, it can restore the vim working environment to the editing environment before you exit. For the currently enabled tab groups, you can use the vim session feature to enable them in the next Edit to quickly restore them to the previous working status, in this way, the unique project management function of IDE can be modeled in Vim.

To restore the last editing environment, we need to save the session information and viminfo information:

    • The views of all windows and global settings are saved in the session information;
    • The viminfo information contains the command line history, search string history, input line history, non-empty register content, file location tag, recent search/replacement mode, buffer list, and global variables..

For details about how to use the vim session function, see Vim session and info in easwy. To make it easier to use the vim session function, we recommend that you use the sessionman. Vim plug-in.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.