Unix Linux VI vim manual |VI vim frequently used commands

Source: Internet
Author: User

Unix Linux vi Vim User manual

About Vi

Vi is the most popular full-screen document editor in the Unix world, and almost any UNIX machine can say
will provide this software. Linux Of course also has, its VI is actually Elvis (copyright question), but they all
Almost. Familiar with the DOS of the document processing, may feel that VI is not good, Unix has developed
Many newer, better-used clerical editors, but not necessarily every Unix machine will install these additional
of software. Therefore, the basic operation of Learning VI is still a good thing, let you in various machines handy.

Vi Basic Concepts

Unix offers a range of ex editors, including ex, Edit and VI. Relative to the full screen editor, now
It may be hard to imagine how to use EX, edit this column editor (someone has used a DOS version 3.3 previously attached
EDLIN? )。 Vi is originally intended to be "Visual," an editor that responds immediately, which means
To see the results of the operation immediately.

Also because VI is a full-screen editor, it has to control what is displayed on the entire terminal screen. and finally
There are many kinds of end-machine, the characteristics are not the same, so VI need to know what kind of end is used now
End Machine. This is set by the term of this environment variable, set the environment variables to see the use of the shell
The description. (unless the Unknow terminal type is responded to when VI is executed, it can be used without setting.) )

As long as the simple implementation of VI can enter the VI editing environment. There's a rough outline of it before you actually do it.
The solution will be better. Vi has two modes, input mode and instruction mode. Input mode is used to enter text
The instruction mode is used to release some of the instructions for arranging files, archiving, and leaving VI, and so on. When
When you execute VI, the instruction mode is entered, and any characters you enter are treated as instructions.

Enter and leave

To enter VI can be directly under the system prompt type VI file name >,VI can automatically help you load
The file you want to edit or open a new file. After entering VI, there will be a wave symbol on the left of the screen.
The symbol indicates that the column is currently empty. To leave VI you can type in instruction mode: Q,: the WQ command is
Archive and leave (note the colon). To switch to instruction mode, use the [ESC] key, if you don't know now
is in what mode, you can press more than a few times [ESC], the system will beep to determine the mode of entry instructions.

Vi Input Mode

How do you enter data? There are several instructions to enter the input mode:

New (Append)
A new data is started from the back of the cursor, and the data behind the cursor moves with the new data.
A new data is started from the end of the column where the cursor is located.

Inserting (insert)
I insert the data from the front of the cursor, and the data after the cursor moves with the new data.
I Insert the data before the first non-whitespace character in the column where the cursor is located.

Start (Open)
o Add a new column under the Cursor column and enter the input mode.
O add a column above the cursor column and enter the input mode.

The narrative may seem a little complicated, but you can see how it works as soon as you actually do it. Real
is important, especially when it is possible to try and verify the results of computer-related things. It is highly recommended to actually use it and
Do not just the K file, only a multiplier effect. (Note: This paragraph is nonsense.) )

Vi Basic Editing

With the function keys on the general keyboard, such as the arrow keys, [Insert], [Delete] and so on, now you should have
can use VI to deal with the text material. Of course VI also provides many other functions to make the text processing more shape
Convenient, interested to see Hostler please continue.

Let's have BCC before we go on. Computers have many cards, different hardware and operating systems. Nor is the PC
Only IBM PCs and their compatible products. In fact, including keyboards, terminals and so on often have different rules
Pane What does this mean?

On the document editing software will encounter such problems, some computer keyboard does not have a specific number of function keys! So no
There are some features that can't be used? This is the same problem on Unix systems, and almost every major computer manufacturer has its own
Unix system, and Vi's method of operation will be somewhat out of the way. Of course, we can use the PC's keyboard
The operation of the Ming VI, but still have to mention this problem.

Delete and modify

What is editing? In this case, we consider the addition and deletion of the text, even the moving and re-
System and so on. Here first introduce VI how to do delete and modify. (Note: In the original concept of VI, the input and
The editor is two things. The edit is operated in instruction mode, and the cursor is positioned to be edited using the instruction movement
Place, and then make an order for editing. )

x Delete the character that is located in the cursor.
DD deletes the column where the cursor resides.
R modifies the character of the cursor, and R is followed by the character to be corrected.
R enters the replacement state and the new data overwrites the original data until you press [ESC] back to instruction mode.
s deletes the character of the cursor and enters the input mode.
S deletes the column where the cursor is located and enters input mode.

In fact, there is no such trouble at all on the PC! Both the input and the edit can be done in input mode. For example, to delete
In addition to the characters, press [Delete] directly. The insert state and the substitution state can be cut directly with [insert]
The editing instructions for the instruction mode. But as mentioned earlier, these instructions are almost every
Terminals can be used, not just on the PC.

The basic instructions for moving a cursor in instruction mode are H, J, K, L. I think you should be able to guess now, as long as straight
The key of the PC is OK, and it can be used in both instruction mode and input mode. How easy it is not.

Of course, the PC keyboard also has shortcomings. There is a very good command u can recover deleted data, and U instruction
You can restore all changes to the column where the cursor is located. This is the same as the [Undo] key function on some computers.

Vi Advanced Applications

I believe that the VI should have a considerable understanding of the present. There is no trouble in dealing with words. If there is a hing
Use the other functions of VI to further simplify the operation, you may wish to continue to watch.

Moving cursors

Because many editing jobs are positioned by cursors, VI provides many ways to move cursors, which we
A few short tables to illustrate (these are, of course, instructions in instruction mode):

┌ ————— ┬ ————————————— ┬ ————— ┐
│ Instruction │ Description │ function key │
├ ————— ┼ ————————————— ┼ ————— ┤
│0│ moves to the front of the column where the cursor is located │[home]│
├ ————— ┼ ————————————— ┼ ————— ┤
│$│ moves to the last face of the column in which the cursor is located │[end]│
├ ————— ┼ ————————————— ┼ ————— ┤
│[ctrl][d]│ down half page ││
├ ————— ┼ ————————————— ┼ ————— ┤
│[ctrl][f]│ down one page │[pagedown]│
├ ————— ┼ ————————————— ┼ ————— ┤
│[ctrl][u]│ up Half page ││
├ ————— ┼ ————————————— ┼ ————— ┤
│[ctrl][b]│ up one page │[pageup]│
└ ————— ┴ ————————————— ┴ ————— ┘
┌--┬ ————————————————— ┐
│ Instruction │ Description │
├--┼ ————————————————— ┤
│h│ move to the first column of Windows │
├--┼ ————————————————— ┤
│m│ move to the middle column of the window │
├--┼ ————————————————— ┤
│l│ move to the last column of the window │
├--┼ ————————————————— ┤
│b│ move to the first letter of the next word │
├--┼ ————————————————— ┤
│w│ move to the first letter of the last word │
├--┼ ————————————————— ┤
│e│ move to the last letter of the next word │
├--┼ ————————————————— ┤
│^│ move to the first non-whitespace character in the column where the cursor is located │
└--┴ ————————————————— ┘
┌--┬ ——————————————————— ┐
│ Instruction │ Description │
├--┼ ——————————————————— ┤
│n-│ minus sign moves to the first non-whitespace character in the previous column │
││ Front plus number can be specified to move to the N column above │
├--┼ ——————————————————— ┤
│n+│ plus moves to the first non-whitespace character in the next column │
││ Front plus number can be specified to move to the following n columns │
├--┼ ——————————————————— ┤
│ng│ directly with the number n plus the upper case G to nth column │
└--┴ ——————————————————— ┘
┌ ———— ┬ ——————————————— ┐
│ Instruction │ Description │
├ ———— ┼ ——————————————— ┤
│fx│ move right to X-characters │
│fx│ move left to x characters │
├ ———— ┼ ——————————————— ┤
│tx│ move right to the X-character before │
│tx│ move left to X-character │
├ ———— ┼ ——————————————— ┤
│ │ cooperate with f&t, repeat once │
│,│ with f&t use, the opposition to repeat once │
├ ———— ┼ ——————————————— ┤
│/string│ move right to the place with string │
│?string│ move left to the place with string │
├ ———— ┼ ——————————————— ┤
│n│ with/&? Use, repeat once │
│n│ with/&? Use, opposite to repeat once │
└ ———— ┴ ——————————————— ┘
┌ ———— ┬ ——————————————————— ┬ —————————— ┐
│ Instructions │ Instructions │ Remarks │
├ ———— ┼ ——————————————————— ┼ —————————— ┤
│n (│ left parenthesis move to the front of the sentence │ the sentence is to │
││ Front plus number can be specified to move the n sentence forward │!. ? Three kinds of symbols to define │
│n) │ Right parenthesis move to the front of the next sentence ││
││ Front plus number can be specified to move n sentences ││
├ ———— ┼ ——————————————————— ┼ —————————— ┤
│n{│ left bracket moves to the front of the paragraph │ paragraph is to │
││ Front Plus number you can specify to move forward n paragraphs │ blank column boundaries between paragraphs │
│n}│ right parenthesis moves to the front of the next paragraph ││
││ Front plus number can be specified to move n paragraphs ││
└ ———— ┴ ——————————————————— ┴ —————————— ┘

Do not attempt to recite these instructions, or you will be responsible for your own consequences. They look so messy and they're actually words
An impediment to the narrative itself. Once again, the actual use of it can only a few times without the brain directly release these odd
Strange instructions, far more than the rote live back to make a much more vague. (Note: If you do not accidentally delete the data through the brain
Nothing to do with me. Another, this paragraph is nonsense. )

Advanced editing Instructions

These editing instructions are very resilient and basically consist of directives and scopes. For example, DW is deleted by means of the
Make d with the range W, representing the deletion of a word D (elete) W (ORD).

The instruction list is as follows:

D Remove (delete)
Y copy (Yank)
P Placement (put)
C Modification (change)

The range can be a few of the following:

e cursor position to the last letter of the word
W cursor position to the first letter of the next word
b cursor position to the first letter of the previous word
$ cursor position to the last letter of the column
0 cursor position to the first letter of the column
) cursor position to the first letter of the next sentence
(The cursor is in the same position as the first letter of the sentence
The cursor is located to the last letter of the paragraph
{The cursor is in the same position as the first letter of the paragraph

To be honest, combining these instructions to edit the file has a little bit of artistic flair. Anyway, they offer more
The ability to edit text. It is important to note that both delete and copy will place the contents of the specified range in the staging area,
Then you can use the command p to post to other places, which is VI to deal with section copy and move the method.

Some vi versions, such as the Elvis used by Linux, can greatly simplify this lump of instructions. If you look a little
These editing instructions will find that the problem is actually fixed range of the way a little miscellaneous, actually only four instructions.
Command V is very easy to use, as long as you press the V key, the cursor position will be reversed, then you can move the cursor to
Set the range, then directly under the command to edit.

For the whole column operation, VI also provides more convenient editing instructions. The previous mention of deleting the entire column of text refers to the
DD is one of them, CC can modify the whole column of text, while YY is to copy the whole column of text, instruction D can be
To delete the cursor until the end of the column.

File Instructions

File instructions to: Beginning, with the editing instructions a little different. For example, the previous instruction to end the edit is: Q.
Let's briefly explain the end of the story as follows:

: Q End Edit (quit)
To discard an edited file if you do not want to archive it by: q! Forced to leave.
: W Archive (write)
You can then add the file name you want to archive.
File instructions can be combined, for example: Wq is archived and then left.
The ZZ function is the same as: Wq.

VI vim commonly used command detailed

1, VI of the basic concept
Basically, vi can be divided into three states, namely command mode, insert mode (and bottom line mode), and the function of each mode is as follows:
1) command-line mode
Controls the movement of the screen cursor, the deletion of characters, words, or lines, moving and copying a section and entering insert mode, or to last line mode.
2) Insert mode
Only in the Insert mode, you can do text input, press "esc" to return to the command line mode.
3) Bottom Line mode
Save or Exit VI, or you can set the editing environment, such as looking for a string, listing line numbers ... such as

2, VI of the basic operation
A) Enter VI
After the system prompt symbol to enter VI and file name, go to the VI Full screen editing screen:
$ VI myfile
However, it is important to note that after you enter VI, you are in command mode and you want to switch to insert mode to enter text. First Use VI people will want to use the next key to move the cursor, the result of the computer has been a beep, the gas to death, so enter the VI, do not move, switch to "insert mode" and then!

b) switch to insert mode to edit the file
Press the letter "i" under Command mode to enter "Insert Mode", and you can start typing the text again at this point.

c) Toggle of Insert
You are currently in "Insert Mode", you can only enter text, if you find the wrong word! To move back with the cursor key, delete the word, then click the "esc" key to go to "command mode" and then delete the text.
d) Exit VI and save the file
Under Command mode, click the ":" colon key to enter "last line mode", for example:
: w filename (enter "w filename" to save the article with the filename you specified)
: Wq (input "wq", save and Exit VI)
: q! (Enter q!, do not save the mandatory exit VI)

3. Command mode function key
1). Insert mode
Press "i" switch into insert mode "insert mode", press "I" into the insertion mode is the beginning of the cursor from the current position to enter the file;
After pressing "a" into insert mode, the text is entered from the next position at the current cursor position;
When you press "o" to enter insert mode, you insert a new line and enter text from the beginning of the beginning.

2). Switch from insert mode to command line mode
Press the "esc" key.

3). Move the cursor
VI can be directly on the keyboard with the cursor to move up and down, but the normal VI is the lowercase English letter "h", "j", "k", "l", respectively, control the cursor left, bottom, upper, and right one grid.
Press "ctrl"+"b": The screen moves one page to the back.
Press "ctrl"+"f": The screen moves one page to the front.
Press "ctrl"+"u": The screen moves the half page toward the back.
Press "ctrl"+"d": The screen moves the half page toward the front.
"0" by number: Moves to the beginning of the article.
Press "g": Move to the end of the article.
Press "$": Moves to the end of the line where the cursor is located.
Press "^": Moves to the beginning of the line where the cursor is located
Press "w": Cursor jumps to the beginning of the next word
Press "e": Cursor jumps to the end of the next word
Press "b": Cursor back to the beginning of the last word
Press #l: The cursor moves to the first # position of the line, such as: 5l,56l.

4). Delete text
"x": Each time you press the cursor, delete the "after" character at the position where the pointer is located.
#x: For example, "6x" represents the "after" 6 characters of the location where the cursor was deleted.
"x": Uppercase X, each time it is pressed, deletes the "front" character at the location of the cursor.
"#X": for example, "20x" represents the "front" 20 characters of the location where the cursor was deleted.
"dd": Delete the line where the cursor is located.
"#dd": Deletes the # line starting at the line where the cursor is located
5). Copy
"yw": Copies the characters from the cursor to the end of the word into the buffer.
"#yw": Copy # words to Buffer
"yy": Copy cursor line to buffer.
"#yy": for example, "6yy" represents a copy of 6 lines of text from the line where the cursor is located.
"p": Paste the characters in the buffer to the cursor location. Note: All copy commands related to "Y" must be mated with "P" to complete the copy and paste function.

6). Replace
"r": Replaces the character at which the cursor is located.
"r": Replaces the character where the cursor is located until the "esc" key is pressed.

7). Reply to Last action
"u": If you mistakenly execute a command, you can press "u" immediately to go back to the previous action. Multiple replies can be performed by pressing "U" multiple times.

8). Change
"cw": Change the word-to-tail at the cursor location
"c#w": For example, "c3w" represents a change of 3 words

9). Jump to the specified line
"ctrl"+"g" lists the line number of the line where the cursor is located.
"#G": for example, "15g", which means moving the cursor to the beginning of the 15th line of the article.
4. Next line Mode command brief
Before using "last line mode", remember to press "esc" to make sure you are under "command mode" and then press ":" Colon to enter "last line mode".

A) List line numbers

"set nu": After you enter "set nu", the line numbers are listed before each line in the file.

B) jump to a line in the file

The "#":"#" number represents a number, enter a number after the colon, and then press ENTER to jump to the line, such as entering the number 15, and then enter, you will jump to the 15th line of the article.

C) Find characters

"/Keyword: First press the"/"key, and then enter the character you want to look for, if the first keyword is not what you want, you can always press"n"will look back to the keyword you want.

"? keyword": Press "?" first Key, and then enter the character you want to find, if the first keyword is not what you want, you can always press "n" will look forward to the keyword you want.

D) Save the file

"w": Enter the letter "w" the colon to save the file.
E) Leave VI

"q": Press "q" is to exit, if you cannot leave VI, can be "q" followed by a "!" Forced to leave VI.

"qw": Generally recommended to leave, use with "w", so you can also save the file when exiting.

5. VI Command List
1. The following table lists the functions of some keys in the command mode:

H
Move left cursor one character

L
Move the cursor one character to the right

K
Move the cursor up one line

J
Move the cursor down one line

^
Cursor moves to the beginning of the line

0
Number "0", the cursor moves to the beginning of the article

G
Cursor moves to the end of the article

$
Move cursor to end of line

Ctrl+f
Flip the screen forward

Ctrl+b
Flip the screen backwards

Ctrl+d
Turn Half-screen forward

Ctrl+u
Turn Back half screen

I
Insert a character before the cursor position

A
The next character at the cursor position begins to increment

O
Insert a new row, starting at the beginning of the line

Esc
Fallback from input state to command state

X
Delete the character following the cursor

X

# characters after the cursor is deleted

X
(uppercase x), delete the character in front of the cursor

X

Delete the # characters before the cursor

Dd
Delete the line where the cursor is located

Dd

Remove the # line from the number of lines that the cursor contains

yw
Copy a word where the cursor is located

yw

Copy the # Word where the cursor is located

Yy
Copy a row where the cursor is located

Yy

Copy the # line from the number of lines where the cursor is located
P
Paste

U
Cancel operation

cw
Change a word in the position of your cursor

cw

Change the # Word where the cursor is located

2, the following table lists some instructions in the line command mode
W filename
Save the file you are editing as filename

WQ filename
Save the file being edited as filename and exit VI

Q!
Discard all changes, exit VI

Set Nu
Show line Numbers

Or?
Find, enter what you are looking for in/after

N
with/or, if the search is not the keyword you are looking for, press N or backwards (with/to) or forward (with?) to continue looking until you find it.

For the first use VI, there are a few notes to remind:
1. When you open a file with VI, you are in command mode, and you want to switch to insert mode to enter text. Toggle method: Click on the letter "i" "Command mode" to enter "Insert Mode", then you can start to enter the text.
2, after editing, you need to switch from insert mode to command line mode in order to save the file, switch method: Press the "esc" key.
3. Save and exit the file: Enter in command mode: WQ! (Don't forget to wq the front)

Unix Linux VI vim manual |VI vim frequently used commands

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.