Basic commands of Linuxvi editor

Source: Internet
Author: User
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. The vi editor is identical for any version of Unix and Linux systems, so you can...

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. 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 editor 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 mode)
Controls the movement of the screen cursor, the deletion of characters, words, or rows, the movement of copying a segment and entering the Insert mode, or to the last line mode.
2. Insert mode)
You can enter text only in Insert mode. press ESC to return to command line mode.
3. last line mode)
Save the file or exit vi. you can also set the editing environment, such as searching strings and listing row numbers... .

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. Go to vi
After the system prompts you to enter the vi and file name, you will be taken to the vi full screen editing screen:

$ Vi myfile
Note that after entering vi, you are in command mode. you must switch to Insert mode to enter text. 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 tamper with the mouse, switch to "Insert mode!

B. switch to Insert mode to edit the file
Click "I" under "command mode" to enter "Insert mode". then, you can enter the 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.
Under "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" to save the disk and exit vi)
: Q! (Enter q !, Force exit vi without saving the disk)

3. command mode function key
1. Insert mode
Press "I" to switch to the insert mode "insert mode". press "I" to enter the insert mode. the input file starts from the current position of the cursor;
After you press "a" to enter the insert mode, the text is entered 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 and text is entered from the beginning of the line.
2. switch from Insert mode to command line mode
Press ESC.
3. move the cursor
Vi can be moved up, down, left, and right 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 back half of the page.
Press ctrl + d to move the screen half to the 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 the end of the row where the cursor is located ".
Press ^ to move the cursor to the beginning of the row"
Press w to jump to the beginning of the next word
Press "e": Move the cursor to the end of the next word
Press "B": the cursor returns to the beginning of the previous word
Press # l to move the cursor to the # position of the row, such as 5l and 56l.

4. delete text
"X": each time you press the button, the "next" character of the cursor is deleted.
"# X": for example, "6x" indicates deleting the "next" 6 characters of the cursor position.
"X": uppercase X. each time you press it, the "front" character of the cursor is deleted.
"# X": for example, "20X" indicates the position where the cursor is located, which is a string of 20 characters.
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 at the end of the cursor 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 the cursor position.
"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" to return to the previous operation. You can perform multiple replies based on multiple "u" requests.
8. change
"Cw": change the word at the cursor to the ending point.
"C # w": for example, "c3w" indicates that three words are changed.
9. jump to the specified row
Ctrl + g lists the row numbers 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: "0". move the cursor to the beginning of the article.
G move the cursor to the end of the article
$ Move the cursor to the end of the row
Ctrl + f front screen flip
Ctrl + B flip back
Ctrl + d move forward half screen
Ctrl + u flip back half screen
I insert characters before the cursor position
A starts to increase after the cursor position.
O insert a new row and start from the beginning of the row.
ESC switches from input to command
X. delete the 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 deletes the row where the cursor is located.
# Dd delete the number of rows from the cursor # rows
Yw copies a word at the cursor position.
# Yw: Copy the # characters at the cursor position
Yy: Copy the row where the cursor is located
# Yy copy the number of rows from the cursor # rows
P paste
U cancel operation
Cw: a word used to change the position of the cursor.
# Cw # Words for changing the position of the cursor
2. some commands in the following table Travel command mode

W filename stores 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 display 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 a file with vi, it is in command mode. you must switch to Insert mode to enter text. Switch method: Click "I" under "command mode" to enter "Insert mode )」, now 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.