0. Preamble

Long period of time programmers spend most of the development tools may be the editor, and a very convenient and efficient for developers editor is very effective. In unix / linux, the windows or even the next, vim can be said to be a very good editor. Although the process of developing many of his friends are using vim, but usually only very limited use of the features vim, vim does not come into contact with many very much in line with demands of the developers. Following on from the developer point of view, frequently used to introduce the development process to function. After reading this article, you will find the original vim is so powerful integrated development environment.

Vim is based on the GPL open-source project is to upgrade the version of vi, and vi is the most common unix environment editor. However, because of the powerful features vim, and many of the linux version of the latest release has the vim as the default vi editor. If you have not installed the system vim, can be downloaded from the vim.sourceforge.net appropriate version. To gossip less, following orders from basic editing, general editing commands, such as the development of commonly used commands introduced vim. To introduce the main function of this article, use the details of more than try to understand. The following discussion on vim 6.2.

1. The basic editing commands

Simple classification of this section the basic vim editing commands, only the function of a simple description is given as a command to use Info vim, vim a detailed description please see the online help. Vim, enter: h <command> can be. The following command in the normal mode (that is, any state ESC key several times more than by the state after the arrival, in order to avoid the concept of vague, here to describe these non-state) input sequence of characters.

Order
Meaning
Order
Meaning

Move

h

j

k

l
Left on the right (and the same as the meaning of the arrow keys)
H

M

L
The same page (screen) in the first line of the middle line,

The last line of

ctrl + f

ctrl + b
Next page

Previous
ctrl + d

ctrl + u
The second half of page

Half page

b

w

e
A word before the first

A word after the first

After the end of a word
0

^

$
The first line

The first line of non-empty character

End-of-line

Input

a

A
After the current character input

End-of-line input
i

I
Characters before the current input

Before the importation of

o

O
Under the current line, insert a new line, type

The current line, insert a new line, type
: r [filename]
Insert the paper under the current row [filename] the contents of

Change

d [range]
Delete the scope of the text, such as: dw (delete the suffix) d $ (delete to end-of-line)
c [range]
Change to delete the designated area directly into the edit state

~ [Scope]
Change the character or the scope of the current case
J
Connect the current row and next row

r [char]
Replacement characters, use the next character to replace the current input character
R
Into the replacement pattern, enter the text to replace characters in text

y [range]
Copy the text within the scope of
p
Paste after current character

x
Delete the current character
.
Repeat last text change command

Find

f [char]

F [char]
Find a trip to the character after the [char]

Look forward line character [char]
t [char]

T [char]
Find a trip to the character after the [char] before

Look forward line character [char] before

/ [string]

? [string]
Down to find [string]

Up to find [string]

N continue to use search to find, the use of N in the opposite direction to continue to find
*

#
Cursor down to find the current word

Look up the current word the cursor

n

N
Repeat the last /? Command

The opposite direction to repeat the last time /? Command

Other

: e filename
Close the current file, edit the document
: w filename
Written document

ctrl + g

: g
File information
: help [cmd]
See the vim command cmd help

: u
undo a change
: red
Undo redo changes

U
undo all the changes in the current row

Note:

[Range] is to define the scope of the previous action, vim inside as motion. Command to change the type of the above motion are ordered, meaning moving the cursor position.

Order
Meaning
Order
Meaning

0
The first character to the line
^
To line the first non-space characters

$
To the last character in line

[n] k

[n] j
Up a few lines

Down a few lines
[n] w
Delete n words

Above classification is a personal memory for ease of classification, there are no specific standards or rules.

2. General orders

2.1. Search

Search should be said that the development process is a very commonly used commands. The most common search order is / and?, To be followed by search terms or regular expressions (see brief summary of the following). And then use n or N to find the next or previous search results.

Development process is most likely to see a word I hope to find it, this time * (the downward current word search) and # (the current search word up) order on the very convenient. If you want to search words contained in the current term can be used can be g * or g # command.

To find the definition of variables, you can use gd (the current definition of the term of the local variables) and gD (the current definition of the term global variable) order, the two orders is not necessarily very accurate, but sometimes it is quite useful.

The current macro definition of the word, you can use [_ctrl-d to find the current word first appeared in the position after the # define. And [d shows the current document and the document contains the current word's first definition of a macro, [D list all the macro definition. ] The beginning of the command is to start the search from its current location.

In the current document and the document contains the first search, the search can use the above macro definition of that order, function, variable, such as a statement, citing the search, you can use [i (start searching from the document that contains the current location of the word) order, [I list all the lines. ] The beginning of the command is to start the search from its current location. Vim command or more consistency. Skip the location is used [_ctrl-i (from the beginning of the first line), or] _ctrl_i (from its current location to start).

Multi-file search can be through an external command: grep to achieve, and use the default grep command is very similar to the list to find out, you can use Quickfix category in the list command to cycle access, through such orders can be in a number of grep Jump to the order list, such an order when the initial design for the compiler error, and 3.4 in order to introduce a detailed description of the relevant.

Order to replace a longer format, but sometimes very useful, it is important to understand every detail in order to protect the flexible use. Command format is as follows:

: [range] s [ubstitute] / (pattern) / (string }/[&][ c] [e] [g] [p] [r] [i] [I] [count]

This command is in the range of meaning, to find the definition of the mode of pattern replaced with string.

Range definition can refer to: help range, usually specified number of rows, marking, and even a search can also be specified, for example:

3. +1 From line 3, after the current line of his

'T, $ -2 from the tag after the end of t to the two lines of the article

% Of all articles, which is equivalent to., $

., / (pattern) / from the current line to the next line matching pattern

pattern and the search mode described above can be a string or regular expressions.

String string usually is, can refer to other special content: help sub-replace-special.

Finally, the meaning of the markings are as follows:

& Keep the command to replace the previous mark

c to confirm each replacement

g of each line of the model for all matching replacement, to replace only the first default line

i Ignore case

I do not Ignore case

2.2. Regular Expression

Use regular expressions greatly enhance the search, a description of the replacement capacity, but also increased the difficulty of learning. Here may be used to develop the process of marking out a simple click, and then give you a few examples, we should be able to learn by analogy, more or less know how to use. Matching mode of learning can refer to: help pattern.

Order
Meaning
Order
Meaning

ordinary atom

^

\ _ ^
Match the first line must be started in the pattern

Matching anywhere in the first line
$

\ _ $
Match end-of-line, must be in the final pattern

End-of-line anywhere match

.

\ _.
Matches any character, not at the end of

Any local matching characters
\ <

\>
The first word

Suffix

multi items

*
0 or more
\ +
1 or more

\?
0 or 1
\ (n, m)
n to m, and try to match the majority of

\ (-n, m)
n to m, and try to match a small number of months

character classes

\ s

\ S
Blank characters <space> <TAB>

Non-blank characters
\ d

\ D
Digital

Non-numeric

\ x

\ X
Hexadecimal digits

Non-hexadecimal digits
\ a

\ A
Letters a-zA-Z

\ w

\ W
Word characters including letters, numbers and underscores
\ h

\ H
The first letter and underscore characters

Other

\ e

\ t

\ r

\ b

\ n
<ESC>

<TAB>

<CR>

<BS>

<NewLine>
\ c

\ C
Ignore case

Match case

This identifier c language you can use \ <\ h \ w * to match;

\ <ca \ w * 5 match to the beginning of the middle term ca for any word that contains 5;

2.3. Multi-window

Sometimes the process of developing a number of documents need to open or look at different parts of a document, which is so multi-window function in the.

The use of open a new window: split [filename] command to add the file name to open a window for editing the file, or edit the current file.

Close the current window use: q to.

Ctrl-w j a window

Ctrl-w k to a window

ctrl-w = all window high and wide

ctrl-w + to increase the current window height

ctrl-w - to reduce the height of the current window

Different file data in memory is a buffer, a buffer can have one or more window, or no window. Jump in between multiple files (described later), the file is usually still in the memory buffer. Can be used: buffers are listed all the current memory buffer.

Use: bn (a buffer) and: bp (a buffer) in the inter-cycle buffer.

2.4. Visual mode

Graphical interface using too many may feel that order to define the scope of the use of non-intuitive, visual model allows the choice of a visual text, and then enter the relevant orders to its instructions. The use of the process is moved to block the beginning of characters, type the command to enter visual mode; the use of mobile command moved to the end block; input related to the contents of the command to operate on this block.

v to enter visual mode of characters to choose

V to enter visual line mode choice

Ctrl-v to enter visual block mode select

2.5. Tag (mark)

Of a tag line is sometimes a very useful function. Can facilitate the movement after this line, in the motion can also quote in this location.

Tag set to use m (a-zA-Z) Set a marker, the name is followed by the characters, a file can be defined within a total of 26 markers az, documents can be defined among a total of 26 markers AZ.

Moved to the tag line, use the '[char] or `[char] command to move to [char] the corresponding tag.

2.6. File Recovery

Things can not knock a half-day lost due to power outages. Vim default enter 200 characters or 4 seconds idle automatically saved in. Swp file.

The usual step is to restore, move to the file directory; use vim-r [filename] to restore file filename, if the editor does not save the file using vim-r "" can; to save the recovered file to another file, use diff program (can use vimdiff) to see if the right to resume. If you do not remember the file name, use vim-r list all the exchange of documents.

3. The development of commonly used commands

3.1. Tag

There can be very convenient after the tag of the source code in a number of document Jump to the characteristics of very convenient to browse, read the code.

C / C + + source code can use the ctags-R *. c *. cpp generated tag file, ctag has been included in the latest released version of vim. With tag files, you can Jump the convenience.

Use vim-t [tagname], vim can be directly open the appropriate file and navigate to the location of tagname. In vim using the command: tag [tagname] can be tagname Jump to the definition, tagname can use regular expressions for fuzzy matching.

Use ctrl-] can directly jump to the current definition of the term if the current is not a tag, use the right side of the first tagname. Beating the course of the composition of the tag stack along the stack SKIP command is ctrl-t, along the stack is moved up and down the command: tn (of old),: tp (new item). The use of tag stack are listed in: tags.

3.2. Preview window

Function call, it is usually very need to see the parameters of function, use the tag technology and the preview window can easily achieve this function.

Command: ptag [tagname] in the preview window shows the definition of Jump to tagname. Current term as a tagname, can directly enter the ctrl-w), in the preview window to view the current definition of the word.

Close the preview window using: pclose command.

In the preview window open a file, use: pedit [filename] command. In the current document and the document contains a word search, and display in the preview window, you can use: psearch [name]. Usually use the search function prototype order.

3.2. Mobile

vim default a very good way to support a wide range of indentation. Indentation can be adjusted using the <(left indent) or> (right indent), the scope can be added in front of or behind the increase in motion.

Use = (motion) command to automatically indent a text.

To find the corresponding {},[],(), sometimes is too much trouble, use the% command can be very convenient to find the corresponding symbol, in addition to the above groups of symbols, also can be / * * / or # if # ifdef # else # elif # endif and so on.

Block of code is usually covered by the () in each block of code between the mobile is very convenient. [(Cursor moved to the first block,]) moved to where the tail piece. Use the command [[Jump to the most outer layer of the (,]] Jump to the most outer), usually the outermost layer of C / C + + program function of the border. The use of the second layer of ([m order to use the second layer)] m order, usually the second java process is the function of the boundary layer.

Or document in the document between the jump, and sometimes need to return to the last cursor location. Can use the command ctrl-o Jump to the previous position, ctrl-i command Jump to a new location. Use the command: jumps are listed in a list of all Jump. These orders include Jump Jump to tag, Jump to tag, search, replace,%, more than one party or the movement.

3.3. Help

Programming, we often need to use the man [section] funcname to view the help of a system function in vim use [section] K command directly view the current term of the manpage.

If you do need to implement a command shell, you can not leave the terminal vim environment and the current circumstances, the use of:! <cmd> To execute a shell command, it is necessary to get a shell, you can use: shell.

3.4. Compilers

That vim can be integrated development environment that can facilitate the realization of its editors - compilers - the cycle of editing. If your procedure makefile, use: make command to compile, but it is more convenient to identify vim compiler error output, and navigate to the appropriate location. Command: cc [num] revealed that the first num complete error message.

Moving in the wrong list, you can use: cn (under a mistake), and: cp (an error). Are listed in order to use the whole list error: clist.

Vim save a list of a number of errors, the use of the old list of errors: colder, the use of a new error list: cnewer.

These commands are known as the QuickFix command, grep command described above also use these commands.

4. References

1. Vim homepage http://vim.sourceforge.net/

2. Vim document http://vimdoc.sourceforge.net/ Home

3. Vim user manual is also online help documentation vim, very comprehensive. http://vimdoc.sourceforge.net/dion/vimum.html

4. Vim of the book, talked about in great detail. http://www.truth.sk/vim/vimbook-OPL.pdf

5. About the author

mounton@www.ihere.org the current focus on network security product development, research; the software development process and so on. You can contact him mount0n@yahoo.com.

mounton@www.ihere.org (mount0n@yahoo.com)

June 2003