Vim Advanced Command

Source: Internet
Author: User
Tags imap

1. Find
/xxx (? xxx) means searching the entire document for a string matching xxx,/means looking down, representing
look up. where xxx can be a regular expression, about the formal type is not much to say.
generally case-sensitive, if you want to be case-insensitive, you must first enter
: Set ignorecase
after finding it, enter N to find the next match, and enter N to search in the opposite direction.
* (#) When you rest the cursor on a word, enter this command to find the one that matches the word
Next (UP) a word. Again, enter N to find the next match, enter n negative
To find. Eliminate the hassle of entering words, that is, finding the word in the cursor
g* (g#) This command is similar to the previous command except that it does not exactly match the word at which the cursor is located, and
is to match all strings that contain the word.
GD This command finds words that match the word in which the cursor is located and pauses the cursor in the document's non-
The first occurrence of this word in the comment segment. Suitable for locating variables at the definition

% This command finds the parentheses that match the cursor location, including () [] {}

2. Move the cursor quickly
In VI, moving the cursor and editing is two things, because of the distinction, so it is very convenient to do the cursor
Bits and edits. So it's useful to move the cursor a bit 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 bank.
^ Move the cursor to the character at the beginning of our line.
$ move the cursor to the end of the bank.
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 tail line of the screen.
GG Moves the cursor to the first line of the document.
G Move the cursor to the end of the document line.
C-f (that is, the CTRL key and the F key are pressed together) This command is page down.
C-b (that is, the CTRL key and the B key are pressed together, and then the same) This command is page up.
"This command is very useful, it moves the cursor to the previous mark, such as with GD, * and so on
Once a word is found, enter it again to go back to where it was last stopped.
‘. This command is quite good, it moves the cursor to the last modification line.
 `. This command is quite powerful, it moves the cursor to the last modification point.(located below ESC)
3. Copy, delete and paste
In vi, y means copy, d means delete, and p means paste. Where copy and delete are with the cursor move command
Combined, see a few examples to understand.
YW represents the copy of the content from the current cursor to the end of the word at the cursor.
DW represents the deletion of content from the current cursor to the end of the word at the cursor.
Y0 represents the copy from the current cursor to the beginning of the cursor.
D0 indicates the deletion of content from the current cursor to the beginning of the cursor.
y$ represents the copy from the current cursor to the end of the line at which the cursor is located.
d$ means to delete the content from the current cursor to the end of the line.
YfA represents the copy of the contents from the current cursor to the first a character following the cursor.
The DFA represents the deletion of the contents from the current cursor to the first a character following the cursor.
In particular:
YY indicates the line where the copy cursor is located.
DD indicates the row where the cursor is deleted.
D means to delete the content from the current cursor to the end of the line at which the cursor is located.
The complex usage of the copy, delete and paste is related to the register and can be queried on its own.
4. Numbers and commands
In VI, the combination of numbers and commands often means repeating this command, if it appears at the beginning of the extension mode, indicates that the row
Number positioning. Such as:
5FX indicates the 5th x character after the lookup cursor.
5w (e) Move the cursor to the next five words.
the 5YY represents the following 5 lines of the copy cursor.
5DD means the following 5 lines are removed from the cursor.
Y2FA represents the copy of the contents from the current cursor to the second a character following the cursor.
: 12,24y represents the copy of the contents between rows 12th through 24th.
: 12,y represents the contents of the copy line 12th to the line where the cursor is located.
: 24Y indicates the contents of the copy cursor line to line 24th. Delete similar.
5. Fast input characters
In VI, you are not required to enter every character, there are many ways to quickly enter some characters.
Students who use Linux/unix must have an experience typing the first few characters and then pressing the command line
The TAB system automatically adds the remaining characters and prints if there are multiple matches. This is the famous order.
(In fact, Windows also has the file name completion function). There are many strings in the VI command, very convenient.
c-p (c-n) in edit mode, enter a few characters before entering this command then VI starts up (bottom) search
The words that match the beginning of the rope are filled, and the command is repeatedly entered to iterate through the search. This command
will be matched in all files opened in this Vim program.
C-x-l in edit mode, this command quickly complements the contents of the line, but only the text that appears in this window
Match in the document.
C-x-f in edit mode, this command indicates the file name. As input:
When you enter this command after/usr/local/tom, it will automatically match:
/usr/local/tomcat/
ABBR is abbreviated. This is a macro action that allows you to use one abbreviation in edit mode instead of another
String. For example, writing Java files often input System.out.println, which is
Is trouble, so you should use abbreviations to reduce the typing. You can do this:
: abbr sprt System.out.println
After entering SPRT and then entering other non-alphabetic symbols, it will automatically expand to system.
Out.println
6. Replace
Replacement is the strong point of VI, because a regular expression can be used to match a string. Here are a few examples.
: s/aa/bb/g replace AA with BB in all strings containing AA that appear on the line where the cursor is located
: s/\/bb/g replaces all AA occurrences of the cursor with BB, replacing only the word AA.
:%s/aa/bb/g replaces AA in all occurrences of a document with a BB in a string containing AA
: 12,23s/aa/bb/g replaces AA with BB in all strings containing AA from 12 rows to 23 rows
: 12,23s/^/#/will add # characters from Line 12 to line 23
:%s= *$== Remove any extra space at the end of the line
: g/^\s*$/d deletes all empty lines that do not contain characters (spaces are not included).
7. Multi-file editing
It is convenient to open many files for editing in a vim program.
: SP (: VSP) filename vim splits a horizontal (vertical) window and opens a new file in that window.
Starting with vim6.0, the file name can be the name of a directory, so vim will
Open the directory and display a list of files, press Enter on the file name to play in this window
Open the file, if you enter O in the new window opens the file, enter? Can see
To help information.
: E file name vim will open the new file in the original window, if the old file has been edited, will be required to save.
C-w-w Vim split several windows what to do? Enter this command to position the cursor in a loop
into each window.
: LS This command to see how many files have been opened in this Vim program, at the bottom of the screen
The following data is displayed:
1%a "usevim.html" line 162
2 # "xxxxxx.html" line 0
which
1 indicates the number of open files, which is useful.
%a represents the file symbol,% represents the currently edited file,
# indicates the last edited file
"Usevim.html" indicates the file name.
Line 162 indicates the cursor position.
: b Serial Number (code name) This command opens the file with the specified ordinal number (codename) in this window, where the serial number (codename)
Just use the: LS command to see.
: Set diff This command is used to compare two files and can be used
: VSP filename
command to open another file, and then enter this command in each file window to see
to the effect.
8. Macro substitution
VI can not only use abbr to replace the text, you can also make the macro definition of the command. Some commands are hard to lose.
So I've defined them in--so it's convenient. These configurations can be pre-written to ~/.VIMRC
(under Windows $VIM/_VIMRC), write in without writing the preceding colon.
: Nmap:nohls Cancel the highlight of the searched string
: Nmap w Command mode to transfer the cursor to a different window
: Running in IMAP input mode
: nmap:%s= *$== Remove any extra spaces at the end of the line.
: IMAP Ibid.
: Java: (note, why this is said in Java, because the following definitions do not work for other file formats,
Will talk about how to achieve this)
: Nmap:comp javac:mak-d. %
This command compiles the Java file with Javac, which automatically navigates the cursor to the error point. But it needs to be fixed.
A Javac.vim file in the $VIM/compiler, in the Javac.vim there are only two lines of words:
Setlocal Makeprg=javac
setlocal errorformat=%a%f:%l:\%m,%-z%p^,%-c%.%#
: Nmap:comp Ant:mak
This command compiles the Java file with ant, which automatically positions the cursor to the point of error. In general, installation
Vim already has the Compiler/ant.vim file, so this command can be used directly. But need
There are build.xml files in the current directory, and of course you must install ant.
: NMAP:CL This command is used to view all compilation errors.
: IMAP
: NMAP:CC This command 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 cut out a new window in the left part of the window, where the contents are the Java Resource tree, including
The classes that appear in this file, the member variables of the class, and the member methods, are like the jcreator performance.
Enter in this window? will see Help. Hey, it is very useful, but need ctags support.
: IMAP
9. TAB
tab is a tab, and it's a separate section because it's really useful.
> enter this command to move the cursor to the right by one tab.
5>> Enter this command, the 5 lines after the cursor move one tab to the right.
:12,24> This command moves data from rows 12 through 14 to the right by one tab.
:12,24>> This command moves 12 rows to 14 rows of data to the right by two tab.
So how do you define the size of the tab? Some people would like to use 8 spaces, some 4, some 2.
Some people want tabs to be completely replaced by spaces, and others want tab to be tab. It's okay, vim can
Help you. The following settings are generally written in the configuration file before they can be knocked on the old.
: Set shiftwidth=4 setting automatically indents 4 spaces, of course, to set automatic indentation first.
: Set sts=4 sets Softtabstop to 4. After you enter the tab, you jump 4 cells.
: Set tabstop=4 The actual tab is 4 spaces, not the default of 8.
: Set Expandtab When you enter tab, VIM fills the tab with the appropriate space.
Ten. Autocmd
This command is very powerful, you can use this command for different file formats to apply different configurations; OK
Automatically add a copyright notice when you create a new file, and so on. These commands are typically defined in a configuration file such as ~/.VIMRC
Inside. Because he is very strong, so I can not give a very specific explanation, can only give a few examples, detailed please see Help.
: autocmd! Remove 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 I open a Java file.
Autocmd bufnewfile *.java 0r ~/.vim/files/skeletons/java.skel
The above command allows me to automatically add the contents of the Java.skel file when I create a new Java file.
Autocmd Bufnewfile *.java Normal GNP
The above command allows me to run the GNP command automatically when I create a new Java file, and this command makes some specialization
Processing, such as replacing __date__ in a new Java file with today's date or something.
11. Common Scripts
In vim.sf.net you can find a lot of scripts that often have unexpected effects on your scripts.
I used to have:
Jcommenter.vim automatically adds Javadoc-style annotations.
Jbrowser.vim class resource browsing. C, C + + etc. can be used Tlist
There are many useful, such as CHECKSTYLE.VIM can test your programming style, Jad.vim can directly
Decompile. class files, and so on.
12. Common Configuration
In the ~/.VIMRC configuration file, you often need some personalized configuration. For example, some of the macro definitions written above, some
Autocmd definition and so on. Like what:
Set Suffixes=.bak,~,.o,.h,.info,.swp,.aux,.bbl,.blg,.dvi,.lof,.log,.lot,.ps,.toc
When you open a file in vim, it ignores the file when you press the TAB key to make the filename.
Set NU Display line number
Set AI settings Auto indent
Map y y$ let y and D, otherwise y is the same as the original meaning of yy.
13. Other
There are many interesting commands that are recorded here lest you forget.
. Repeats the last edit command.
: G/^/exec "s/^/". Strpart (Line ("."). " ", 0, 4) Insert row numbers at the beginning of the line
: runtime! Syntax/2html.vim convert txt into HTML and will follow your color configuration to go

Vim Advanced Command

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.