VIM usage skills
Author: camry.wu@gmail.com
I am a Vim fan, I have used it for a long time, there are some own feelings, and I have dug everywhere to some people's skills. I feel the Vim
Fans are more useful, just remember it here. I hope that I will take out my own skills, I will have ancient bricks.
.
Let's introduce Vim. VI is a very common text editor under UNIX / Linux, most of the machines
Yes. VI has a variety of variants, commonly used different variant software on different machines. Among them, VIM is more useful.
Wide. Vim is the abbreviation of VI Improved, indicating better Vi. I personally think it is a very good editor (for
It is best to avoid Emacs fans, it will not say the best). It is also recommended to see it, of course, Vim is writing text.
The file is convenient to use, such as writing programs, HTML documents, etc., but cannot be used to write Word documents.
Regarding the installation of Vim, you can search for many online, if you are not here, if you have Vim
Interest, then see here (Chinese document): http://vcd.cosoft.org.cn/pwiki/index.php
This article says it is more useful, more common commands. If you can use these commands, you will find editing files.
Very comfortable.
Description:
In the following example, XXX is indicated in the command mode and enter the XXX and carries it.
In the following example: XXX indicates the input XXX and carries in the extended mode.
The commands in small brackets represent relevant commands.
The command entered in editing mode or visual mode will also be indicated.
Find
/ xxx (? xxx) indicates that searching for the XXX string in the entire document, / indicates the down look, indicating
Find up. The xxx can be a regular expression, and there is not much to say about the regular shape.
Generally speaking, it is case-sensitive.
: set ignorecase
After finding it, enter N to find the next match, enter the N anti-direction lookup.
* (#) When the cursor is on a word, enter this command indicates that the lookup matches the word.
Next (on) a word. Similarly, enter N to find the next match, enter N-counter
Look for.
G * (g #) This command is similar to the upper command, but it does not completely match the words where the cursor is located.
It is a string that matches all strings that contain the word.
GD This command finds the words match the word where the cursor is located, and the cursor stays in the document
The place where this word appeared in the first time in the comment section.
% This command finds the hinges that match the cursor, including () [] {}
f (f) x This command indicates that the row is in the cursor, find the first X character of the right (left) party.
After finding:
Enter; indicating that continue
Enter, indicate the opposite direction
2. Fast moving cursor
In VI, moving cursors and editing are two things, because distinguishing, it can be convenient to perform light calibration
Bit and editing. It is useful to move a cursor faster.
W (e) moves the cursor to the next word.
b Move the cursor to the previous word.
0 Move the cursor to the beginning of the Bank.
^ Move the cursor to the most beginning character of the Bank.
$ Move the cursor to the end of the Bank.
H Move the first line of the cursor to the screen.
M Move the cursor to the middle of the screen.
L Move the cursor to the tail of the screen.
GG Moves the cursor to the documentation.
G moves the cursor to the document tail line.
C-F (ie, a Ctrl key is pressed with the F button) This command is Page Down.
C-B (ie the CTRL button is pressed with the B key, then the same) This command is Page Up.
'' This command is quite useful, it moves the cursor to the previous mark, such as GD, *, etc.
After finding a word, enter this command back to the last stay. '. This command is quite good, it moves the cursor to the last modifier line.
`. This command is quite powerful, it moves the cursor to the last modification point.
3. Copy, delete and paste
In the VI, Y indicates the copy, D represents the deletion, p is indicated. Where copy and deletion is the command with the cursor
In combination, you can understand a few examples.
YW indicates the content of the copy from the current cursor to the word where the cursor is located.
DW indicates the content that deletes the end of the word where the current cursor is located.
Y0 indicates that the copy from the current cursor to the leader of the cursor.
D0 means that deletes content from the current cursor to the cursor.
Y $ indicates that the copy from the current cursor to the tail of the cursor.
D $ represents the content that deletes the tail of the current cursor to the cursor.
Yfa represents the content between the first A character behind the current cursor to the cursor.
The DFA indicates the content between the first A character from the current cursor to the cursor.
specially:
YY indicates that the copy cursor is located.
DD indicates that the delete cursor is located.
D indicates the content that deletes the tail of the current cursor to the cursor.
Compared with the copy, delete, and paste is related to the register, you can query it.
4. Digital and command
The number and command combination in the VI tend to repeat the command, and if the beginning of the extended mode appears,
Number positioning. Such as:
5FX indicates the 5th x character after the cursor is found.
5W (e) Move the cursor to the next five words.
5yy represents a copy of the cursor below 5 lines.
5DD indicates that delete 5 rows below the cursor.
Y2FA represents the content between the copy from the current cursor to the second A character behind the cursor.
: 12, 24y represents the content between the 12th lines to 24th lines.
: 12, y indicates that the contents of the 12th line to the row of the cursor are.
:, 24Y indicates the content between the line to the 24th line of the copy cursor. Delete is similar.
5. Quick input characters
In VI, you don't require you to enter each character, you have many ways to quickly enter some characters.
Classmates using Linux / UNIX must have an experience, knocking in the head in several characters when entering the command.
The Tab system will automatically replenish the remaining characters. If there are multiple matches, it will be printed. This is the famous command
Etil (in fact, there is also a file name replenishment function in Windows). There are many string tuning commands in VI, which is very convenient.
C-P (C-N) In editing mode, enter several characters and then enter this command, VI starts to search up (below)
Cars with their matching words and complement, constantly enter this command, loop lookup. This command
Match in all files opened in this VIM program.
C-X-L In editing mode, this command quickly complements the entire row content, but only appears in this window.
Matching in the documentation.
C-X-f In editing mode, this command represents a file name. Such as input:
/ usr / local / TOM, enter this command, it will automatically match:
/ usr / local / Tomcat /
Abbr is abbreviated. This is a macro operation, you can use an abbreviation in editing mode to replace another
String. For example, writing the Java file often input system.out.println, this is very
It is trouble, so you should use an abbreviation to reduce knocking. Can do this:
: abbr sprt system.out.println
In the future, enter the SPRT and then enter other non-alphabetic symbols, it will automatically expand to System.
Out.println
6. Replace
Replacement is the strength of VI because you can match the string with a regular expression. A few examples are provided below.
: S / AA / BB / G Replace all AAs in the row of the cursor in the AA string of AA to BB: S / /
:% S / AA / BB / G replace all AAs in the AA string in the document to BB
: 12, 23S / AA / BB / G will replace all AA in the AA string from 12 lines to 23 to BB
: 12, 23s / ^ / # / will join the top of 12 lines to 23 rows # characters
:% s = * $ == Delete all walking spaces
: g / ^ / s * $ / d Remove all space lines that do not contain characters (spaces not included).
7. Multi-file editing
Open a lot of files in a VIM program is very convenient.
: The SP (: vsp) file name Vim will split a horizontal (vertical) window and open a new file in this window.
Starting from VIM6.0, the file name can be the name of a directory, so that Vim will
Turn this directory and display the list of files, press the carriage return on the file name to play on this window.
Open this file, if you enter O, open this 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 edited, save it.
What should C-W-W VIM segment several windows? Enter this command to locate the cursor
In each window.
: ls This command looks that this VIM program has opened how many files have been opened, at the bottom of the screen
The following data is displayed:
1% a "Usevim.html" line 162
2 # "xxxxx.html" line 0
among them:
1 Represents the file number opened, this serial number is useful.
% a represents the file code,% represents the current edited file,
# Represents the files previously edited
"Usevim.html" means a file name.
Line 162 represents the cursor position.
: b Sequence No. (Code) This command opens the specified number (code) file on this window, the serial number (code)
Just use: LS commands.
: SET DIFF This command is used to compare two files, you can use
: vsp FileName
The command opens another file, then enter this command in each file window, you can see
The effect is.
8. Macro replacement
VI can not only replace the text, but also the macro definition of the command. Some commands are very powerful,
So I define them to
(Under Windows $ VIM / _VIMRC), do not write the previous colon when writing.
: nmap
: NMAP
: IMAP
: NMAP
: IMAP
: Java: (Note, why is this Java, because the following definitions do not work in other file formats, below
Will talk about how to achieve this)
: NMAP
This command compiles the Java file with Javac, which will automatically locate the cursor to the error point. But this need to define a javac.vim file under $ VIM / Compiler, only two lines in Javac.vim:
SetLocal Makeprg = Javac
SetLocal ErrorFormat =% a% F:% L: /% M,% - Z% P ^,% - C%.% #
: NMAP
This command compiles the Java file with ANT, which will automatically position the cursor to the error point. Generally, installation
After Vim already has a Compiler / Ant.vim file, this command can be used directly. But needs
There is a build.xml file in the current directory, of course, Ant must be installed.
: NMAP
: IMAP
: NMAP
: IMAP
: NMAP
: IMAP
: NMAP
: IMAP
: NMAP
This command is used to cut a new window on the left side of the window, and the content inside is Java's resource tree, including
The class, class member variables and members of this document are as follows as the JCREATOR performance.
Enter it in this window? Will see help. Hey, it is very easy to use, but you need CTAGS support.
: IMAP
9. Tab
Tab is a tab and take it out and do it because this thing is really useful.
<< Enter this command, move a Tab left to the left of the cursor.
>> Enter this command, move a Tab at the line where the cursor is located.
5 >> Enter this command to move a TAB to the right after the cursor.
: 12,24> This command moves 12 lines to 14 lines to the right a Tab.
: 12,24 >> This command moves 12 rows to 14 lines to the right two Tabs.
So how do I define the size of the Tab? Some people are willing to use 8 empty grenades, some people use 4, some use 2.
Some people hope that Tab is completely replaced by spaces, and some people hope that Tab is Tab. It doesn't matter, VIM can
Help you. The following settings are generally written to the configuration file first, so as not to be old.
: set shiftWidth = 4 Settings Auto indented 4 spaces, of course, automatic indentation is required.
: set STS = 4 Sets SoftTabStop to 4. After entering tab, you jump 4.
: set tabstop = 4 The actual Tab is 4 spaces, not the default 8.
: SET ExpandTab fills this Tab with the appropriate space after entering Tab.
10. AutoCMD
This command is very powerful, you can use this command to apply different configurations for different file formats;
Automatically add a copyright statement when new files is newly created. These commands generally define configuration files such as ~ / .vimrc
Inside. Because he is very powerful, I can't give a very specific instructions, can only mention a few examples, please see more.: 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 let me 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 join the Java.skel file when new Java files.
AutoCmd bufnewfile * .java Normal GNP
The above command allows me to automatically run the GNP command when new Java files, this command is specialized
Processing, such as replacing __date__ in the new Java file with what is the date today.
11. Common script
In Vim.sf.net You can find a lot of scripts (Script), which often have unexpected roles.
I am common with:
JCommenter.vim automatically joins the Javadoc style annotation.
JBROWSER.VIM resource browsing. C, c , etc. can be used in TLIST
There are also many useful, such as Checkstyle.vim to check your programming style, Jad.vim can be directly
Anti-compilation .class file, etc.
12. Common configuration
In ~ / .vimrc configuration files you often need some personalized configurations. For example, some macro definitions written above, some
AutoCMD definition, etc. For example:
SET Suffixes = .bak, ~, .o, .h, .info, .swp, .aux, .bbl, .lg, .dvi, .lof, .log, .lot, .ps, .toc
In this way, when the file is opened in the VIM, it will ignore the above file when pushing the file name.
Set nu display line number
SET AI sets automatic indentation
Map y y $ allows Y and D, or the same is true and yy.
13. Others
There are also many interesting orders, which are recorded here.
Repeat the last edit command.
: g / ^ / exec "s / ^ /". Strpart ("."). "", 0, 4) Insert the line number in the head
: Runtime! SYNTAX / 2HTML.VIM Convert TXT to HTML, follow you
Color configuration