Vim advanced features + combined functions
This article describes some of the useful and commonly used commands. If you are familiar with these commands, you will find it quite comfortable to edit files.
Note:
In the following example, xxx indicates entering XXX in command mode and press Enter.
In the following example: XXX indicates entering XXX in extended mode and press ENTER
The commands in parentheses indicate related commands.
The command entered in edit mode or visual mode will be noted separately.
1. Search
/XXX (? XXX) indicates searching for strings matching XXX in the entire document,/indicates searching down ,? Indicates
Search up. xxx can be a regular expression, so I won't say much about the regular expression.
Generally, it is case sensitive. To be case insensitive, you must enter
: Set ignorecase
After finding it, enter n to find the next match and enter n to search for it.
* (#) When the cursor stays on a word, enter this command to search for matching words.
Next (top) a word. Similarly, enter n to find the next match and enter n inverse.
To search.
G * (G #) this command is similar to the previous command, except that it does not fully match the word at the cursor.
Is to match all strings containing the word.
GD this command looks for words that match the word where the cursor is located, and points the cursor to the non-
The place where the word appears for the first time in the Comment Segment.
% This command looks for the parentheses matching the cursor location, including () [] {}
F (f) X: This command searches for the first X character on the right (left) side of the cursor in the row where the cursor is located.
After finding:
Input; indicates to continue searching
Input, indicating Reverse Lookup
2. move the cursor quickly
In VI, moving the cursor and editing are two things. Because they are distinguished, you can easily perform optical calibration.
It is useful to move the cursor a little faster.
W (e) move the cursor to the next word.
B. move the cursor to the previous word.
0 move the cursor to the beginning of the line.
^ Move the cursor to the starting character of the line.
$ Move the cursor to the end of the row.
H move the cursor to the first line of the screen.
M moves the cursor to the middle line of the screen.
L move the cursor to the end of the screen.
Gg move the cursor to the first line of the document.
G move the cursor to the end of the document.
C-F (press Ctrl and F together) this command is page down.
C-B (that is, press Ctrl and B together, and then the same) this command is page up.
''This command is quite useful. It moves the cursor to the previous tag, for example, using Gd, *, and so on.
After finding a word, enter this command to return to the position where it was last stayed.
'. This Command makes it very easy to move the cursor to the last modified line.
'. This command is quite powerful. It moves the cursor to the last modification point.
3. Copy, delete, and paste
In VI, y indicates copy, d Indicates delete, and P indicates paste. Copy and delete commands are used to move the cursor.
In combination, you can see several examples.
YW indicates copying the content from the current cursor to the end of the word where the cursor is located.
DW indicates deleting the content from the current cursor to the end of the word where the cursor is located.
Y0 indicates copying the content from the current cursor to the beginning of the row where the cursor is located.
D0 indicates deleting the content from the current cursor to the beginning of the row where the cursor is located.
Y $ indicates copying the content from the current cursor to the end of the row where the cursor is located.
D $ deletes the content from the current cursor to the end of the row where the cursor is located.
Yfa indicates copying the content from the current cursor to the first a character after the cursor.
DFA indicates to delete the content from the current cursor to the first a character after the cursor.
Specifically:
YY indicates copying the row where the cursor is located.
Dd indicates deleting the row where the cursor is located.
D: Delete the content from the current cursor to the end of the row where the cursor is located.
The complex usage of copying, deleting, and pasting is related to registers, which can be queried by yourself.
4. Numbers and commands
In VI, the combination of numbers and commands often indicates repeated execution of this command. If it appears at the beginning of the extended mode, it indicates the line
Positioning. For example:
5fx indicates 5th x characters after the cursor is searched.
5 W (e) move the cursor to the next five words.
5yy indicates copying 5 rows below the cursor.
5dd indicates deleting 5 rows below the cursor.
Y2fa indicates copying the content from the current cursor to the second a character after the cursor.
: 12, 24y indicates copying content between 12th rows and 24th rows.
: 12, y indicates copying the content between 12th rows and the row where the cursor is located.
:, 24y indicates copying the content between the row where the cursor is located and the row 24th. Deleting is similar.
5. Enter characters quickly
In Vi, you do not need to enter every character. There are many ways to quickly enter some characters.
If you are using Linux/Unix, you must have an experience. when you enter a command on the command line, enter the first few characters and then press
The tab system will automatically complete the remaining characters. If there are multiple matches, it will be printed. This is the famous command.
(In fact, file name completion is also available in Windows). VI has many character string completion commands, which are very convenient.
C-P (c-N) in editing mode, enter a few characters and then enter this command, then VI starts searching up (down)
Enter the matching words at the beginning of the cable and fill them in again. Enter this command repeatedly to search for the matching words. This command
All files opened in this Vim program will be matched.
C-X-L in editing mode, this command is used to quickly complete the contents of the line, but only
Match in the document.
In C-X-F editing mode, this command indicates filling up the file name. For example, enter:
/Usr/local/Tom:
/Usr/local/tomcat/
Abbr is short for. This is a macro operation, which can be replaced by an abbreviation in editing mode.
For example, if system. Out. println is often input to write a Java file
It is troublesome, so we should use abbreviations to reduce the number of words. You can do this:
: Abbr SPRT system. Out. println
After entering SPRT and other non-alphanumeric characters, it will be automatically extended to system.
Out. println
6. Replace
Replacement is the strength of VI, because regular expressions can be used to match strings. Below are several examples.
: S/AA/BB/g replace AA in all strings containing aa that appear in the row where the cursor is located with bb
: S/\/BB/g replace all AA in the row where the cursor is located with BB. Replace only the word AA.
: % S/AA/BB/g replace AA in all strings containing AA in this document with bb
: S/AA/BB/g replace AA in all strings containing aa that appear from row 12 to row 23 with bb
: 12, 23 S/^/#/Add the first line from 12 to 23 characters #
: % S = * $ = Delete unnecessary spaces at the end of all rows
: G/^ \ s * $/d delete all blank lines that do not contain characters (spaces are not included.
7. Multi-file editing
Opening and editing many files in a Vim program is quite convenient.
: SP (: VSP) file name Vim Splits a horizontal (vertical) window and opens a new file in the window.
Starting from vim6.0, the file name can be the name of a directory. In this way, VIM will
Open the Directory and display the file list. Press enter on the file name to press
Open the file. If you enter o, open the file in a new window. Enter? You can see
To help information.
: E file name Vim will open a new file in the original window. If the old file has been edited, it will be required to save.
How does C-w Vim split several windows? Enter this command to position the cursor cyclically
To each window.
: Ls this command shows how many files have been opened in the vim program, at the bottom of the screen
The following data is displayed:
1% a "usevim.html" Row 162
2 # "xxxxxx.html" line 0
Where:
1 indicates the number of opened files, which is very useful.
% A indicates the file code, and % indicates the file being edited,
# Indicates the Last edited File
"Usevim.html" indicates the file name.
Row 162 indicates the cursor position.
: B serial number (CODE) This command opens the file with the specified serial number (CODE) in this window, where the serial number (CODE)
The LS command is used.
: Set diff this command is used to compare two files, you can use
: VSP filename
Command to open another file, and then enter this command in each file window, you can see
The effect is reached.
8. Macro replacement
Vi can not only replace text with abbr, but also define macro commands. Some commands are difficult to input,
Therefore, I define them to "-", which is convenient. These configurations can be written in advance ~ /. Vimrc
($ Vim/_ vimrc in Windows), you do not need to write the first colon.
: NMAP: nohls cancel the highlighted string
: Transfer cursor to different windows in nmap w command mode
: Run in IMAP input mode
: NMAP: % s = * $ = Delete unnecessary spaces at the end of all rows.
: IMAP same as above
: Java: (Note: Why is Java mentioned here, because the following definition does not work for other file formats?
Will talk about how to implement this)
: NMAP: comp javac: Mak-D. %
This command uses javac to compile a Java file. It automatically positions the cursor to an error point.
In $ Vim/compiler, A javac. Vim file contains only two lines:
Setlocal makeprg = javac
Setlocal errorformat = % A % F: % L: \ % m, %-Z % P ^, %-C %. % #
: NMAP: comp ant: Mak
This command uses ant to compile the Java file. It automatically positions the cursor to an error point. Generally, the installation
Vim has the compiler/ant. Vim file, so this command can be used directly.
There is a build. xml file in the current directory, and ant must be installed.
: NMAP: CL this command is used to view all compilation errors.
: IMAP
: NMAP: CC this command is used to view the current compilation error.
: IMAP
: NMAP: CN this command is used to jump to the next error location.
: IMAP
: NMAP: CP this command is used to jump to the previous error location.
: IMAP
: NMAP: javabrowser
This command is used to split a new window at the left of the window, which contains the Java Resource Tree, including
The classes, member variables, and member methods in this file are as shown in jcreator.
In this window, enter? We can see the help. Hey hey, it's very useful, but we need the support of ctags.
: IMAP
9. Tab
Tab is a tab, Which is used separately for a section because it is really useful.
> Enter this command to move a tab to the right of the row where the cursor is located.
5> enter this command to move a tab to the right of the 5 rows after the cursor.
:> This command moves the data from 12 rows to 14 rows to the right of a tab.
:> This command moves the data from 12 rows to 14 rows to the right of two tabs.
How can we define the tab size? Some people prefer to use 8 spaces, 4 for some, and 2 for some.
Some people want the tab to be replaced by spaces, and some people want the tab to be tab. It doesn't matter. Vim can
Help you. The following settings are usually written to the configuration file first, so that you do not need to repeat it.
: Set shiftwidth = 4 set Automatic indent 4 spaces, of course, you must set Automatic indent first.
: Set STS = 4: Set softtabstop to 4. After Entering the tab, the four cells are displayed.
: Set tabstop = 4 the actual tab is 4 spaces, instead of the default 8.
: Set expandtab after entering the tab, VIM fills the tab with appropriate spaces.
10. autocmd
This command is very powerful. You can use this command to apply different configurations to different file formats.
The copyright statement is automatically added when a file is created. These commands are generally defined in ~ /. Configuration files such as vimrc
It is very powerful, so I cannot give a very specific description, only a few examples, please refer to the help for details.
: Autocmd! Delete all previous automatic commands.
Autocmd filetype Java source ~ /. Vim/files/Java. Vim
Autocmd filetype Java source ~ /. Vim/files/jcommenter. Vim
The above two commands allow me to apply the two configuration files mentioned later when opening the Java file.
Autocmd bufnewfile *. Java 0R ~ /. Vim/files/skeletons/Java. skel
The above command allows me to automatically add the content of the Java. SKEL file when creating a Java file.
Autocmd bufnewfile *. Java normal GNP
The above command enables me to automatically run the GNP command when creating a Java file. This command is made special
For example, replace _ date _ in the new Java file with the date of today.
11. Common scripts
In vim.sf.net, you can find many scripts that often have unexpected effects.
I often use the following methods:
Jcommenter. Vim automatically adds javadoc-style annotations.
For jbrowser. Vim resources, you can use tlist to browse. C and C ++ resources.
There are also many useful ones, such as checkstyle. Vim, which can test your programming style, and Jad. Vim can directly
Decompile. class files and so on.
12. Common configurations
In ~ /. You often need some personalized configurations in the vimrc configuration file. For example, some macro definitions and some
Autocmd definition. For example:
Set suffixes =. Bak ,~,. O,. H,. info,. SWP,. aux,. BBL,. BlG,. DVI,. LOF,. Log,. Lot,. ps,. TOC
In this way, when opening a file in Vim, it will ignore the above files when you press the tab key to complete the file name.
Set nu display row number
Set AI sets automatic indent
Map y $ let y and d be the same, otherwise y's intention is the same as yy.
13. Others
There are also many interesting commands recorded here to avoid forgetting.
. Repeat the last edit command.
: G/^/exec "s/^/". strpart (line ("."). "", 0, 4) Insert row numbers at the beginning of a row
: Runtime! Syntax/2html. Vim converts TXT to HTML, which is converted according to your color configuration.