Basic usage of the VI Editor --

Source: Internet
Author: User

Basic usage of the VI Editor --

From: Linux community


The VI editor is a standard editor for all UNIX and Linux systems. It is not inferior to any of the latest text editors. Here we just briefly introduce its usage and a few instructions. Because
The VI editor is identical for any version of UNIX and Linux systems, so you can learn more about it in any other section about VI. VI is also the most basic text encoding in Linux
After learning it, you will be able to enjoy the free access in the Linux World.

1. Basic concepts of vi
Basically, VI can be divided into three states: Command mode, insert mode, and last line mode. The functions of each mode are as follows: 1) command line mode) controls the movement of the screen cursor, the deletion of characters, words, or lines, the movement of a certain segment, the entry into the insert mode, or to the last line mode. 2) The insert mode can be used for text input only in the insert mode. Press the ESC key to return to the command line mode. 3) The last line mode saves or exits the file VI. You can also set the editing environment, such as searching strings, listing row numbers, and so on ...... . However, we generally simplify VI into two modes in use, that is, the last line mode is also included in the command line mode ). 2. Basic operations of vi
A) after entering VI and the file name in the system prompt symbol, go to the VI full screen editing screen: $ VI myfile
However, after entering Vi, you are in the command mode. You need to switch to the insert mode
Mode. People who use VI for the first time will want to move the cursor first with the upper and lower right keys. As a result, the computer keeps beeping and getting angry with themselves. So after entering VI, do not change it
"Insert mode! B) switch to insert mode. Click "I" under "command mode" to enter "insert mode) ", then you can start entering text. C. Insert switch: You are currently in "insert mode", and you can only enter text all the time. If you find that you have entered an error! To move the word back with the light mark key, you must first Press ESC to switch to command mode and then delete the text. D) Exit VI and save the file. In command mode, click ":" colon to enter "last line mode". For example :: W filename (enter "w filename" to save the article with the specified filename): WQ (enter "WQ", save the disk and exit VI): Q! (Enter Q !, Do not save disk force exit VI) 3. command line mode function key
1 ). in insert mode, press I to switch to insert mode. In insert mode, press I to enter insert mode. Then, the input file starts from the current position of the cursor; after you press "a" to enter the insert mode, the text is input starting from the next position where the cursor is currently located. After you press "O" to enter the insert mode, a new row is inserted, enter text from the beginning of the line. 2) switch from insert mode to command line mode and Press ESC. 3 ). move the cursor VI can move up, down, and left directly with the cursor on the keyboard, but the regular VI uses lowercase letters "H", "J", "K", and "L 」, the cursor is controlled to move one cell to the left, down, up, and right respectively. Press Ctrl + B to move the screen to the back. Press Ctrl + F to move the screen to the front. Press Ctrl + u to move the screen to the rear half of the page. Press Ctrl + D to move the screen to front. Press the number "0": to move to the beginning of the article. Press g to move to the end of the article. Press "$" to move to "end of line" of the row where the cursor is located ". Press "^": Move to the "Beginning of the line" of the row where the cursor is located press "W": the cursor jumps to the beginning of the next word and press "E 」: move the cursor to the end of the next word and press "B": the cursor returns to the beginning of the previous word and press "# l": the cursor moves to the # position of the row, for example, 5l, 56l. 4). Delete the text "X": each time you press it, delete the "Next" character at the cursor position. "# X": for example, "6x" indicates deleting the "Next" to the cursor position. "X": uppercase X. Each time you press it, the "front" character of the cursor is deleted. "# X": for example, "20x" indicates to delete the "front" of the cursor. DD: Delete the row where the cursor is located. "# Dd": delete from the row where the cursor is located # Row 5). Copy "yw": copy the character from the cursor to the end of the character to the buffer zone. "# Yw": Copy # words to the buffer zone "YY": copy the row where the cursor is located to the buffer zone. "# YY": for example, "6yy" indicates copying 6 lines of text from the row where the cursor is located "down. P: place the characters in the buffer to the cursor position. Note: All copy commands related to "Y" must work with "P" to complete the copy and paste function. 6) Replace "R": Replace the character at which the cursor is located. "R": Replace the character wherever the cursor goes until you press the "ESC" key. 7). Reply to the previous operation "U": If you mistakenly execute a command, you can immediately press "u" and return to the previous operation. You can perform multiple replies for multiple times by "U. 8 ). change cw: change the word at the cursor to C # W at the end of the word. For example, c3w indicates changing 3 words 9 ). jump to the specified row Ctrl + G to list the row number of the cursor. "# G": for example, "15g" indicates moving the cursor to the first row of the article.

4. Introduction to commands in last line mode
Before using "last line mode", remember to press the "ESC" key to confirm that you are already in "command mode" and then press 「: "colon to enter" last line mode 」.

A) List row numbers

"Set nu": After "set nu" is entered, the row number is listed before each row in the file.

B) jump to a row in the file.

"#": "#" Indicates a number. Enter a number after the colon and press enter to jump to the row. For example, enter the number 15 and press Enter, this will jump to the second line of the article.

C) Search for characters

"/Keyword": First press the "/" key and then enter the character you want to search. If the first search keyword is not what you want, you can always press "N" until you find the keyword you want.

「? Keyword: First press 「?」 Enter the character you want to search for. If the keyword you want for the first time is not what you want, you can press "N" until you find the keyword you want.

D) save the file

"W": Enter "W" in the colon to save the file.

E) Leave Vi

Q: Press Q to exit. If you cannot exit Vi, you can follow Q with 「!」 Force exit VI.

"QW": it is generally recommended to use it with "W" when leaving, so that files can be saved when exiting.

5. VI command list
1. The following table lists some key functions in command mode:

H
Move the cursor one character left

L
Move the cursor one character to the right

K
Move the cursor up a row

J
Move the cursor down a row

^
Move the cursor to the beginning of the line

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

G
Move cursor to the end of the article

$
Move the cursor to the end of the row

CTRL + F
Forward screen flip

CTRL + B
Flip back

CTRL + d
Front Half Screen

CTRL + u
Flip back half screen

I
Insert characters before the cursor position

A
Add the next character at the cursor position

O
Insert a new row and start from the beginning of the row.

ESC
Return from input to command status

X
Delete characters after the cursor

# X
# Characters After deleting the cursor

X
(Uppercase X), delete the character before the cursor

# X
Delete # characters before the cursor

Dd
Delete the row where the cursor is located

# Dd
Delete # rows from the number of rows where the cursor is located

YW
Copy a word at the cursor position

# YW
Copy the # characters at the cursor position

YY
Copy a line at the cursor position

# YY
Copy the number of rows from the cursor

P
Paste

U
Cancel operation

CW
Change the cursor position.

# CW
# Words for changing the cursor position

2. Some commands in the following Table travel command mode
W filename
Save the file being edited as filename

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

Q!
Discard all modifications and exit vi

Set nu
Show row number

/Or?
Search, enter the content to search after/

N
And/or? If the search content is not the keyword, press N or backward (and/) or forward (and? Continue searching until it is found.

Note the following when using VI for the first time:
1. After opening the file with VI, it is in command mode. You need to switch to insert
Mode. Switch method: click "I" under "command mode" to enter "insert mode ".
Mode) ", then you can start to input text.
2. After editing, you must switch from the insert mode to the command line mode to save the file. to switch to the mode, Press ESC.
3. Save and exit the file: In command mode, enter WQ! (Don't forget the one before WQ :)

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.