Use Vi Editor

Source: Internet
Author: User
Use the Vi Editor-general Linux technology-Linux programming and kernel information. For more information, see the following. Since its generation, Vi has been continuously innovated. Now the latest version of Vi has powerful features, making it easier for users,
It is easy to use.

6.1 vi Working Mode

6.1.1 command mode

When you start to enter Vi, it is in command mode. If you are already in insert mode or last line mode, press The key can return to the command
Mode. In this mode, only key commands can be used, and text cannot be entered.

6.1.2 insert mode
In the insert mode, the text is inserted into the file to be edited, and the Insertion Location varies according to the command used. From command mode
To enter the insert mode, enter commands such as I, a, o, r, and I, A, O, and R. After entering the text, you must use Key return command mode.

6.1.3 last row Mode
The last line is named because the command appears at the bottom of the screen. In command mode, type some special characters, such /,? ,:
Wait, the cursor jumps to the last line of the screen and displays the character of the last line. Enter the command and press Enter. vi will display a certain response information in the last line as needed, the command status is automatically returned.

6.2 Vi Basic file editing process
Type vi testfile in the command line. testfile indicates the name of the file to be opened. If the file does not exist, vi will automatically create
A file named testfile.
After entering vi, press I to enter the insert mode, and you can write a file. The cursor can be moved by the direction key. You can delete the previous character from the key.
If the file has been written, you can press Return to the command mode and archive the file (note that it is a colon command ).
If you want to exit vi, you can press: q or use q together to save and leave.

6.2.1 move the cursor
Note: All operations in command mode are described in this section.

6.2.1.1 move the cursor
Left h Backspace or left direction key
Enter j or + or the arrow key
Top k-or top direction key
Right l Space or right direction key
Ctrl + f PageDown
Turning up Crtl + B PageUp
6.2.1.2 complex cursor Movement
0 to the beginning of the row, or Key
~ Move to the first non-blank character
$ Move to the end of a row, or Key
G move to the end of the file (the first non-blank character in the last line)
Move gg to the beginning of the file (the first non-blank character in the first line)
W move to the next beginning
W is the same as above, but some punctuation marks are ignored.
E. Move to the end of the previous word.
E is the same as above, but some punctuation marks are ignored.
B Move to the first word
B is the same as above, but some punctuation marks are ignored.
H move to the first non-blank character at the top of the screen
M moves to the first non-blank character in the middle of the screen
L move to the first non-blank character at the bottom of the screen
N | move to the nth character
: N or n G to the beginning of the n line. Note: n indicates a specific number, such as 1, 2, 3 ......
) To the beginning of the next sentence.
(Move to the beginning of the previous sentence
} Move to the beginning of the next paragraph
{Move to the beginning of the previous paragraph

6.2.2 basic editing commands

6.2.2.1 insert mode command
I start to input text (insert) before the character where the cursor is located)
A starts to input text (append) after the character where the cursor is located)
O open a new line under the row where the cursor is located)
I. Enter text at the beginning of the row.
A. Enter text at the end of A row.
O open a new line on the row where the cursor is located to input text
J. Connect the next row to the row (joint)

6.2.2.2 DELETE command
X deletes the character at which the cursor is located. Also available Key.
X deletes the character before the cursor position.
Dd deletes a whole row.
Dw deletes a word (delete word ).
Delete dG to the end of the file.
D to the end of the line, or d $ (including the character at the cursor position ).

6.2.2.3 replacement and Restoration
R replaces the character at which the cursor is located.
Replace the character R until you press <Esc>.
Cc replaces the entire line of content. Or uppercase S.
Cw replaces an English text.
~ Converts the case sensitivity of the cursor.
C is replaced by the end of the line, that is, all words after the cursor is located will be replaced. Or c $.
C0 is replaced by the first line, or c ~.
U undo: undo. There is no limit on the number of undo operations.
U replies to all edit actions before the cursor leaves the row.

6.2.2.4 copy
Yy copies the entire row of the cursor. Or an uppercase Y.
Copy 2yy or y2y.
Y ~ Copy to the beginning of the line, or y0, without the character where the cursor is located.
Y $ copy to the end of the row. Contains characters at the cursor position.
Copy one word from Yw.
Copy Yg to the end of the file.
Copy y1G to the beginning of the file.

6.2.2.5 search and replace
Search
/In command mode, press/to display a/in the lower left corner. type the string to be searched and press enter to start searching.
? It is the same as/, but/is the forward (bottom) lookup ,? Then it is to find it backward (up.
N to continue searching.
N continues searching (reverse ).
* Search for the words where the cursor is located (to be fully compliant ).
# Same as above, but * Is to find forward (bottom), # Is to find backward (top.
G * is the same as *, but some are the same.
G # is the same as #, but some of them are the same.
Replace
: [Range] s/pattern/string/[c, e, g, I]
Replace pattern with string. Range refers to the Range. 1, 7 refers to the entire article from the first row to the seventh row, 1, $ refers to the entire article from the first row to the last row. % indicates the Range; c. Ask before each replacement; e. Do Not Display error; g. Do not ask. Replace the whole line;
I is case insensitive.

6.2.3 exit
: Q. If the file is modified but not saved, a warning will be given and the file cannot be left.
: Q! Give up all modifications and force exit.
Q: after saving the file, it will be saved again even if the file is not modified.
: X saves the file and leaves. However, if the file is not modified, the file will not be saved.
: ZZ and: x are identical.
In addition, if the file name is not added, the original file is written.

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.