vim tips
Author: camry.wu@gmail.com
I'm a fan of vim, after a long time, I have some feelings, and I have dug some other people's skills everywhere. Feeling right to vim
Fans are more useful, just write it down here. I hope that this article sets out everyone's own ingenuity, and it is just like the old one
Words.
Let me briefly introduce vim. Vi is a very common text editor under unix / linux, most of the machines are
Yes. There are various variants of vi, and different variants of software are commonly used on different machines. Among them, vim is easier to use and more used
Extensive. Vim is short for Vi IMproved, which means better vi. I personally think it is a very good editor (for
In order to avoid Emacs fans picking their eyes, let ’s not say the best). It is also recommended to try it if you have not used it.
The files are very convenient and easy to use, such as writing programs, html documents, etc., but they cannot be used to write word documents.
Regarding the installation of vim, a lot of basic usage methods can be found on the network, so it is not verbose here, if there is a vim
Interested, then take a look here (Chinese document): http://vcd.gro.clinux.org/
This article will talk about some of the more useful and commonly used commands. If you can use these commands proficiently, you will find editing files.
Very comfortable.
Explanation:
In the following example, xxx means enter xxx in the command mode and press Enter
In the following example: xxx means enter xxx in the extended mode and press Enter
The commands in parentheses indicate related commands.
Commands entered in edit mode or visual mode are noted separately.
1. Find
/ xxx (? xxx) means to search for the string matching xxx in the whole document, / means to look down,? means
Look up. Where xxx can be a regular expression, not much to say about the regular form.
Generally speaking, it is case-sensitive, if you want to be case-insensitive, you must enter
: set ignorecase
After finding, enter n to find the next match, and enter N to search in the opposite direction.
* (#) When the cursor stays on a word, enter this command to find the match with the word
Next (previous) word. Similarly, enter n to find the next match, enter N inverse
Look up.
g * (g #) This command is similar to the previous command, except that it does not exactly match the word under the cursor, and
Is to match all strings containing the word.
gd This command finds the word that matches the word under the cursor, and puts the cursor on the non-document
The first occurrence of this word in the comment section.
% This command finds the back brackets that match the cursor, including () () ()
f (F) x This command means to search on the line where the cursor is, and find the first x character on the right (left) of the cursor.
After finding:
Enter; means continue to look down
Enter, for reverse search
2. Move the cursor quickly
In vi, moving the cursor and editing are two things. Because of the distinction, the cursor can be easily set.
Position and editing. So it is 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 line.
^ Move the cursor to the beginning of the line.
$ Move the cursor to the end of the line.
H Move the cursor to the first line of the screen.
M Move 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 (that is, the ctrl key is pressed together with the f key) This command is page down.
c-b (that is, the ctrl key and the b key are pressed together, the same below) This command is page up.
'' This command is very useful, it moves the cursor to the previous mark, such as using gd, * etc.
After finding a word, enter this command to return to the last stop position.
'. This command is quite good, it moves 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 means copy, d means delete, p means paste. Among them, copy and delete are commands with cursor movement
Combined, it can be understood by looking at a few examples.
yw means copy the content from the current cursor to the end of the word where the cursor is.
dw means to delete the content from the current cursor to the end of the word where the cursor is.
y0 means copy the content from the current cursor to the beginning of the line where the cursor is.
d0 means to delete the content from the current cursor to the beginning of the line where the cursor is located.
y $ means copy the content from the current cursor to the end of the line where the cursor is.
d $ means to delete the content from the current cursor to the end of the line where the cursor is.
yfa means copy the content from the current cursor to the first a character behind the cursor.
dfa means to delete the content from the current cursor to the first a character behind the cursor.
specially:
yy means copy the cursor line.
dd means delete the line where the cursor is.
D means to delete the content from the current cursor to the end of the line where the cursor is.
The complicated usage of copying, deleting and pasting is related to the register, you can inquire yourself.
4. Numbers and commands
The combination of numbers and commands in vi often means that the command is repeated, if it appears at the beginning of the extended mode, it means the line
Number positioning. Such as:
5fx means the 5th x character after the search cursor.
5w (e) Move the cursor to the next five words.
5yy means copy 5 lines below the cursor.
5dd means to delete 5 lines below the cursor.
y2fa means copy the content from the current cursor to the second a character behind the cursor.
: 12,24y means copy the content from line 12 to line 24.
: 12, y means copy the content from line 12 to the line where the cursor is.
:, 24y means copy the content from the line where the cursor is to the 24th line. Delete is similar.
5. Enter characters quickly
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, type the first few characters when typing commands on the command line, and then press
The TAB system will automatically fill in the remaining characters, if there are multiple matches it will be printed out. This is the famous command
Completion (in fact, there is also a file name completion function in Windows). There are many string completion commands in vi, which is very convenient.
c-p (c-n) In edit mode, enter a few characters and then enter this command, vi starts to search up (down)
Search for the matching words at the beginning and complete them. Enter this command continuously to search in a loop. This command
Will match in all files opened in this vim program.
c-x-l In edit mode, this command quickly fills the entire line, but only appears in this window
Match in the document.
c-x-f In edit mode, this command means to complete the file name. For example, enter:
After / usr / local / tom and then enter this command, it will automatically match:
/ usr / local / tomcat /
abbr is an abbreviation. This is a macro operation, you can replace one with an abbreviation in edit mode
String. For example, when writing java files, you often enter System.out.println, which is very
It is troublesome, so you should use abbreviations to reduce typing. You can do this:
: abbr sprt System.out.println
In the future, after entering sprt and other non-letter symbols, it will automatically expand to System.
out.println
6. Replace
Replacement is the strength of vi, because you can use regular expressions to match strings. Here are a few examples.
: s / aa / bb / g replaces aa with bb in all strings containing aa appearing on the line where the cursor is located
: s // <aa /> / bb / g Replace all aa appearing in the line under the cursor with bb, only replace the word aa
:% s / aa / bb / g replaces aa with bb in all strings containing aa in the document
: 12,23s / aa / bb / g replaces aa with bb in all strings containing aa appearing in lines 12 to 23
: 12,23s / ^ / # / will add the # character from the beginning of lines 12 to 23
:% s = * $ == delete all extra spaces at the end of the line
: g / ^ / s * $ / d deletes all blank lines that do not contain characters (including spaces).
7. Multi-file editing
It is convenient to open many files for editing in a vim program.
: sp (: vsp) The file name vim will split a horizontal (vertical) window and open a new file in that window.
Starting from vim6.0, the file name can be the name of a directory, so that vim will
Open the directory and display the file list, press Enter on the file name to type in this window
Open the file. If you enter O, the file will be opened in a new window. Enter? To 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 ask to save it.
What if c-w-w vim splits several windows? Enter this command to position the cursor in a loop
Into each window.
: ls This command checks how many files this vim program has opened, at the bottom of the screen
The following data will be displayed:
1% a "usevim.html" line 162
2 # "xxxxxx.html" line 0
among them:
1 indicates the serial number of the open file, which is very useful.
% a represents the file code,% represents the currently edited file,# Indicates the last edited file
"usevim.html" represents the file name.
Line 162 indicates the cursor position.
: b Serial number (code) This command will open the file with the specified serial number (code) in this window, of which the serial number (code)
I saw it with the: ls command.
: 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
It's time to effect.
8. Macro replacement
vi can not only use abbr to replace text, but also macro definition of commands. Some commands are very difficult to lose,
So I defined them on <F1>-<F12>, which is very convenient. These configurations can be written to ~ / .vimrc in advance
($ VIM / _vimrc under windows), you don't need to write the colon before writing it.
: nmap <F2>: nohls <cr> cancel the highlighting of the searched string
: nmap <F9> <C-W> w Command mode to move the cursor to a different window
: imap <F9> <ESC> <F9> Run in input mode <F9>
: nmap <F12>:% s = * $ == <cr> delete all extra spaces at the end of the line.
: imap <F12> <ESC> <F12> Same as above
: java: (Note, why java is said here, because the following definition does not work for other file formats, below
Will talk about how to achieve this)
: nmap <F3>: comp javac <CR>: mak -d.% <CR>
This command uses javac to compile the java file, it will automatically position the cursor to the error point. But this needs to be fixed
To define a javac.vim file under $ VIM / compiler, there are only two lines in javac.vim:
setlocal makeprg = javac
setlocal errorformat =% A% f:% l: /% m,%-Z% p ^,%-C%.% #
: nmap <F4>: comp ant <CR>: mak <CR>
This command uses ant to compile the java file, it will automatically position the cursor to the point of error. Generally speaking, install
After vim, there is already a compiler / ant.vim file, so this command can be used directly. But it needs
There is a build.xml file in the current directory, of course, ant must also be installed.
: nmap <F5>: cl <CR> This command is used to view all compilation errors.
: imap <F5> <ESC> <F5>
: nmap <F6>: cc <CR> This command is used to view the current compilation errors.
: imap <F6> <ESC> <F6>
: nmap <F7>: cn <CR> This command is used to jump to the next error location.
: imap <F7> <ESC> <F7>
: nmap <F8>: cp <CR> This command is used to jump to the previous error location.
: imap <F8> <ESC> <F8>
: nmap <F11>: JavaBrowser <cr>
This command is used to split a new window in the left part of the window, the content inside is the Java resource tree, including
The classes, member variables, and member methods that appear in this file are as JCreator behaves.
Type in this window? You will see the help. Hey, very easy to use, but need ctags support.
: imap <F11> <ESC> <F11>
9. TAB
TAB is a tab character, and it is taken out separately as a section because this thing is really useful.
<< Entering this command moves the cursor line one tab to the left.
>> Entering this command moves the cursor line to the right by one tab.
5 >> Enter this command, the next 5 lines of the cursor will move one tab to the right.
: 12,24> This command moves the data from line 12 to line 14 one tab to the right.
: 12,24 >> This command moves the data from line 12 to line 14 two tabs to the right.
So how to define the size of the tab? Some people are willing to use 8 spaces, some people use 4, some use 2.
Some people want tabs to be completely replaced with spaces, and some people want tabs to be tabs. It does n’t matter, vim can
To help you. The following settings are generally written to the configuration file first, so as not to knock.
: set shiftwidth = 4 Set auto indent by 4 spaces, of course, set auto indent first.
: set sts = 4 means to set softtabstop to 4. After entering the tab, it jumps 4 frames.
: 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 implement different configurations for different file formats;
Copyright notices are added automatically when creating new files. These commands are generally defined in configuration files like ~ / .vimrc
Inside. Because he is very powerful, I can not give a very specific explanation, only a few examples, please see 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 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 add the contents of the java.skel file when creating a new java file.
autocmd BufNewFile * .java normal gnp
The above command allows me to automatically run the gnp command when creating a new java file, this command performs some specialization
Processing, such as replacing __date__ in the new java file with today's date or something.
11. Common scripts
You can find many scripts at vim.sf.net, which often have unexpected effects.
I often use:
jcommenter.vim automatically adds javadoc-style comments.
JBrowser.vim resource browsing. C, C ++, etc. can use Tlist
There are many other useful, such as checkstyle.vim can check your programming style, jad.vim can directly
Decompile .class files, etc.
12. Common configuration
In the ~ / .vimrc configuration file, you often need some personalized configuration. For example, some macro definitions written above, some
autocmd definition etc. For example:
set suffixes = .bak, ~, .o, .h, .info, .swp, .aux, .bbl, .blg, .dvi, .lof, .log, .lot, .ps, .toc
In this way, when you open a file in vim, it will ignore the above file when you press the tab key to complete the file name.
set nu display line number
set ai set auto indent
map Y y $ makes Y the same as D, otherwise Y's original meaning is the same as yy.
13. Other
There are also many interesting commands, which are recorded here so as not to forget.
. Repeat the last edit command.
: g / ^ / exec "s / ^ /". strpart (line ("."). "", 0, 4) Insert line number at the beginning of the line
: runtime! syntax / 2html.vim convert txt to html, will follow your
Color configuration
Support Java:
Support for tag jumping
Tags are Java identifiers. They can be package names, class names, method names, or field names. Create a large label with all the tags in your code
Indexing allows you to jump to the definition of any class, method, or field with a single keystroke. These index files are created using the ctags program,
The latter is available in the vim distribution.
Ctags will obtain a list of Java source files or directories containing Java source code, extract the identifier information from it, and then generate the jumps needed by vi
Sorting index to the label. The following line of code is an example of ctags call, as long as the JAVA_HOME variable is correctly defined, it will generate an include
There are tag files for all identifiers in the project source code, as is the Sun core library source code:
ctags -f ~ / .tags -R ~ / myprojects / src $ JAVA_HOME / src
Once the label index is created, all you need to do is tell vi where to find it. This step is done by setting
Add tags = ~ / .tags to your ~ / .vimrc file. With the available label index file, and vi also knows where to find it,
You can now jump to any identifier by pressing [CTRL]]. Although this is for jumping to methods, classes and words you have written
It is very useful in the paragraph, but it will be more useful if you add the Java core library to your tag index. Now, simply use the
Press [Ctrl] on the class or method, and you will jump to the definition it completely uses javadoc. .
Tag name filling
Although jumping to the position where the class, method, and field definitions are located is useful for viewing its javadoc and checking its contents, the more common case is,
You just need to check the definition of a keyword. In most IDEs, this is done by entering keywords. However, if you have
The signature file is set up as described in the previous section, so filling in the keywords in vi is just a guided configuration.
Set
complete =., w, b, u, t, I put this line in your ~ / .vimrc file, which allows you to automatically insert any class
The method or field name is filled. Press [Ctrl] N continuously to move to the next matching place. When you see the tag you want to find, just continue to enter your source
The rest of the code will do. You can find other tips about tag name filling on vim's Web site, including using the Tab key to fill in.
Method of folding
Good practical experience tells us: you should keep the class short and simple, because the complexity of the program is expressed in the class hierarchy, not the 500-line method
on. Unfortunately, some classes have been out of control and become virtually inoperable.
IDEs often provide a table that lists the methods in the content in a narrow box on the left to reduce the difficulty for developers to view lengthy source code.
The opposite is , Vi provides a collapsible program block called "method folding". Folding allows you to hide indented
(Indented) All codes between the beginning and end of a code block. To activate folding, add set foldmethod = indent to your ~ / vimrc.
Once the folding of the method is activated, you can use: set
foldlevel = 0 to greatly streamline a Java source file. If you set the number of folded layers to zero, you are telling vi to display only the command line that is not indented at all, and the latter
Several Java source files represent packaging, import, and class or interface definition lines. Setting foldlevel = 1 tells vi to say: Except for the command lines that are not indented, the commands that are only indented once
The lines should also be displayed. They include the signature of the method and a few other things. To open a folder and view the content hidden by a method, you would enter zO on the folded command line.
By setting the foldlevel, you can easily open or cancel the fold of the method. I like to use it when debugging, but I don't like to use it when writing new code. You have to decide for yourself how it should adapt to the way you work.
IDEs are not just for editing; they are also compilation environments. Vi does a very good job without using an external compiler and profiling the resulting information. Now many Java projects will be easy to script
Ant as a creation environment. Instead of loading a lot of Ant-specific settings into the .vimrc file, I created a separate vimAnt script, see Listing B.
This script calls an Ant executable file with several parameters, and then filters the output to include only the compiler output. You are almost ready to call Ant from vi. You just need to
Listing
The command line in C is added to your ~ / .vimrc file. These command lines tell vi to call the vimAnt script you created and prompt it how to parse Ant's jikes output.
After you have the vimAnt script in your system path, you can now use the: make command to initiate the compilation. The current directory and all its parent directories will be searched for a build.xml file, and the class object of this file will be called. The error output will be automatically analyzed by vi.
Once you have compiled with errors in vi, you can use quick fix (quick
fix) mode to quickly reach the wrong place and make timely repairs. The: cn command will take you to the next error location, regardless of what file it is in. : cc command will show you the current wrong compilation
Output information; and: cl will generate a list of all errors of the project for browsing these errors. Fix your mistakes, you are only one: make away from successful compilation.
There is also a plug-in about vim written by Dian Fox (a cow on Mizuki):
http://learn.tsinghua.edu.cn:8080/2005212716/html/vimplugins.html