Original article address: Two good Vim skillsAuthor: Liurhyme
Note the following before reading this article:
1. The goal of this article is to provide some Vim usage skills, which can improve the efficiency of VIM operations. Some tips can also be used on VI, but now Vim is basically used.
2. this article describes how to sort out and summarize the usage skills, rather than explaining how to get started with vim, so it does not involve basic usage, such as how to move the cursor up or down, for such operations, see any vim or VI tutorial.
3. This article is intended for those who have learned about the basic operations of vim and want to use Vim efficiently. Anyone familiar with vim naturally does not have to read it carefully. If you can leave your valuable comments, I would be grateful.
4. I will collect and sort this article. For more information, see the source.
In general, this article uses (the letters inside are generally case-insensitive, unless otherwise specified) to hold down CTRL and press the relevant letters. Add an I before the command to use this command in insert mode.
1. Select text/copy and paste
V is the visual mode. You can select multiple rows. After multiple rows are selected, you can use YY or dd to copy and cut them.
P is paste
Y and D can directly copy or cut the selected content
YW is a copy of a word.
The simplest way to copy the entire row is V, y, and P.
V can select any character in a row, and V is selected in a row. Multiple rows are selected by moving the cursor down or up one row at a time.
For the values selected by V, after the copy operation is completed, the values will be copied as much as the values selected. For the values selected by V, the paste operation will automatically switch to the next line.
In command mode, it is also the block selection, but the column block is selected.
2. Collapse code
ZF can be used to fold, Zo can be used to open the fold, or the direction key can be used to open the fold to the right, ZC can close the fold (as long as any statement in the collapsed block)
3. indent code
Right indent
After selecting by V, press = to automatically format the code and indent the code. The internal recursive indentation is achieved.
Line left shift>, this command is used to adjust the source code indent format is simple and fast.
4. move the cursor
% Is to move from the beginning of braces to the end of braces
: Adding the row number to the back is to jump to this line.
The cursor returns to the previous position. It is equivalent to a "undo" move by the cursor"
The cursor returns to a later position. It is equivalent to "Restoring" when the cursor moves"
5. Multi-file editing/buffer commands
Each opened file in VIM corresponds to a buffer ).
Multi-file editing involves two situations. One is that the parameter used before entering Vim is multiple files (this case is called argument list ). Another scenario is to open other files (called buffer list) after entering vim ). However, they can all be collectively referred to as buffer.
5.1 open a file
VI flname1 flname2... Flnamen
Call multiple files into the buffer, which is an argument list.
: E filename
After entering vim, open other files without leaving Vim. As long as the file you want to edit is in the current directory, you can still use the tab to complete the key. Is the buffer list.
Note: E or: New or: Split can be followed by the Directory, so that you can slowly find the file to open under the directory.
5.2 buffer jump
: N: edit the next file.
: 2n: Edit two files.
: N: edit the previous file. Note that this method can only be used in the case of argument list.
: Rew back to the first file
: ARGs view the status of the currently edited buffer File
: E # or Ctrl-^ edit the previous file, which is quite useful for mutual editing. This method can be used between argument list or buffer list files. The CTRL-^ command is more convenient, but the function will be invalid if the terminal type is incorrect.
Use: ls to display the files in the buffer. # indicates the previous file, which can be entered through: E #. % A indicates the current file, if there is nothing, you can enter through: bn, where n represents the file number.
: Move the file name or number B to the file.
In: ls, the number of each file is displayed, which will not be changed before vim is left. This command is also available for Elvis. Of course: E # numbers are also acceptable. This rule is applicable to all VI clones.
In buffers, minus sign-indicates that the buffer is not loaded. However, you don't have to worry about it. loading is quite fast. The plus sign + indicates that the buffer has been modified.
: BN buffer next. N indicates the letter N.
: BL buffer last.
The preceding two commands are not applicable to Elvis.
If you use the vim GUI, the buffers option will be available on the menu, so you can easily know and move the buffer.
5.3 remove a buffer
: BD (elete) buffer will not be removed before it leaves Vim. You can use this command to remove it. What is it about to remove it? Vim is loaded only when you call it. Therefore, these buffers do not occupy memory like cache.
5.4 re-edit
Discard a modified file and edit it again.
(1) run the following command: Q! Force exit and then re-enter VI flname.
(2) Use the command: E! Force re-edit is more convenient. In this way, the file will be opened, but the changes to the current edited file will be abandoned. Otherwise, if the file has changed, VIM defaults will not allow you to leave. : E! If nothing is received, all modifications are discarded and the files in the editing are reloaded.
5.5 other commands
: Files or: buffers or: ls lists all files in the current buffer.
In Elvis, you can use B to call buffers.
: F or Ctrl-G: displays the currently edited file name, whether the file is modified, and where the current cursor is located.
: F file name changes the file name in editing. (File)
: R file name inserts the content of a file at the cursor position. (Read)
: The 35 R file name inserts the file into 35 rows.
GF: this is a special method for VIM to open a file. It will open the file named "word" at the place where the cursor is located. Of course, this file must be in the current directory; otherwise, a new file will be created.
6. Search commands
After you use/to search for a word, N can be jumped to the next one, and N is the previous one. nohls can be used to cancel highlighting.
When searching, the: Set ignorecase option causes Vim to ignore case sensitivity and the ": Set noignorecase" option to disable this function.
7. Modify text
CW: deletes a word and enters the insert mode. CC: deletes a row and enters the insert mode.
R: The entered letters will replace the current letter and keep the command mode. R will not stop replacing (one by one ).
0 to the beginning of the line, $ to the end of the line.
8. jump between functions
CTRL +] And Ctrl + T are the definitions and responses of search functions, respectively. It seems that the support of CTAG is required.
I jump to the definition line of the identifier referred to by the cursor. It is to open a new small window and display it. Remember to add an I. It is best to find a file under your project. Otherwise, it is better to find a library function than man.
Capital K to view the man help page of the identifier pointed by the cursor
9. Window commands
= Ctrl + W
: The split File Name displays the content of multiple files on a page at the same time, similar to multiple windows, use to switch the current window
F. Split the file name where the cursor is located. Vim searches for the file name in path. For example, it is often used to open the file in # include statement.
_ When several files are opened at the same time, press _ to maximize the current window
Run Ctrl-W to specify the cursor movement:
CTRL-W + expand window
CTRL-w-zoom out window
CTRL-w h move to the left of the window
CTRL-w j move to the bottom of the window
CTRL-w k move to the top of the window
CTRL-w l move to the right of the window
After you press it, release the keyboard and then press the next command.
If you want to close the split window, you can use close. If there is only one window left, it cannot be closed.
Multiple windows are split, just like opening multiple files with E is to put the files in the buffer zone.
10. Automatic completion
I. Search up and add all the words
I. Search down and add all the words
I.
For example, you have written a line for (INT I = 0; I. If the line is not the one you want, you can search in the file system by or select the last or next matching line.
If the match is not found in the current file after the word is pressed or supplemented, VIM searches for the file in the # include statement, and the file location searches in the path.
I. Copy the characters in the corresponding column of the previous row.
I copied the characters in the corresponding column of the next row. Both of them can be pressed until the end of the row stops.
11. comment out the entire content
Annotation block method:
Select the line I // ESC to comment out
After I, the input is inserted to the front edge of the selected row until ESC.
To remove the annotation: select the annotator //, and then d
After column blocks are selected, I is inserted at the front and a is inserted at the back.
Is selected by column block mode, is selected by row, and V is selected by common
12. Other commands
U can undo the previous operation, CTRL + R can restore
I command to execute a common mode command, return to the insert mode after the execution is complete, no need to run multiple ESC
I. Enter special ASCII characters or keys for subsequent characters.
. Repeat the previous operation at the current position of the cursor
:! Execute an external command on the command line.
**************************************** **************************************** ***
I am a fan of VIM. I have been using it for a long time. I have some feelings and found some other people's skills everywhere. I think it is useful to VIM fans. Remember it here. I hope that this article will help you develop your own clever work, so we will be able to introduce the old saying.
Let's take a look at vim. VI is a widely used text editor in Unix/Linux, which is available on most machines. VI has various variants, and different variant software is often used on different machines. among them, VIM is relatively easy to use and widely used. vim is short for VI improved, indicating better VI. I personally think it is a very good editor (in order to avoid the eye-catching of Emacs fans, it is not the best ). we recommend that you try it if you have never used it. Of course, VIM is easy to use for compiling text files, such as programming and HTML documents, but it cannot be used to compile Word documents.
The installation and basic usage of VIM can be found on the network, so it is not here. If you are interested in Vim, take a look here (Chinese document ):
Http://vcd.gro.clinux.org/
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.
'The 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.