Although Vim has been used to write programs and documents, it has not been used to explore the specific usage 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 following code:
"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: TABP
Map: sorting of the tabn4 Tab
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 tab and session
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. For more convenient use
The sessionman. Vim plug-in is recommended for the vim session function.
Append:
Map to <Ctrl-tab> open next tab, <alt-1> open first tab, <alt-2> open second ,...
If has ("gui_running ")
: Map <silent> <C-S>: If expand ("%") = "" <CR>: browse confirm W <CR>: else <CR>: confirm W <CR>: endif <CR>
Noremap <M-1> 1gt
Noremap <M-2> 2gt
Noremap <M-3> 3gt
Noremap <M-4> 4gt
Noremap <M-5> 5gt
Noremap <M-6> 6gt
Noremap <M-7> 7gt
Noremap <M-8> 8gt
Noremap <M-9> 9gt
Noremap <M-0> 10gt
Noremap <C-TAB> GT
Noremap <C-F4> <ESC>: BD <CR>
Noremap QT <ESC>: BD <CR>
AU bufenter * simalt ~ X "maximum the initial window
Else
Colorscheme desert "torte
Endif