Introduce the use of vi command under linux

One of the most powerful editors - vi vi is a screen editor provided by all UNIX systems, it provides a window device through which you can edit files. Of course, people who know a little about the UNIX system find vi to be very difficult to use, but vi is the most basic editor, so I hope readers can learn it well, and they will be able to run smoothly in the UNIX world in the future. Unhindered and easy, because several other text processors are not standard on UNIX. Maybe someone else's Linux machine doesn't have joe or pico installed. If you don't know vi, then you may not. The basic concept of vi Basically vi can be divided into three operating states, namely command mode (Command mode), insert mode (Insert mode) and bottom line command mode (Last line mode), the functions of each mode are as follows:

1. Comand mode: Control the movement of the screen cursor, delete characters or cursors, move and copy a section and enter Insert mode, or go to Last line mode.

2. Insert mode: Only in Insert mode, text data input can be done. Press Esc to return to Comand mode.

3. Last line mode: will save the file or leave the editor, you can also set the editing environment, such as finding strings, listing line numbers, etc.

However, vi can be simplified into two modes, that is, last line mode is also counted as Command mode, and vi is divided into Command and Insert mode.

Basic operations of vi:

Enter vi After entering vi and the file name at the system prompt, you can enter the vi full-screen editing screen: $ vi testfile One thing to pay attention to is that you are in "Command mode" after entering vi, you need to switch to Insert mode to enter text. Users who use vi for the first time will want to use the up, down, left, and right keys to move the cursor first. As a result, the computer keeps calling, which makes them half-dead, so after entering vi, don't move around first, and then switch to Insert.

Switch to Insert mode to edit files. Press 'i', 'a' or 'o' in Command mode to enter Insert mode. At this point you can start typing text.

i: Insert, insert the entered text from the current cursor position.

a: Increase, the next word where the cursor is located starts to enter text.

o: Inserts a new line, entering text from the beginning of the line.

Insert switch→Command mode, press the Esc key, you are currently in Insert mode, you can only keep typing. If you find a typo and want to use the cursor keys to move back and delete the word, you must press the ESC key to switch back to Command mode, and then delete the text.

Leave vi and save the file in Command mode, you can press the colon ":" to enter the Last line mode, for example: :w filename (input "w filename", save the article to the specified file name filename) :wq (input "wq ”, because the file name testfile has been specified when entering, it will write testfile and leave vi) :q! (input "q!", force to leave and abandon the edited file) Command mode function key list is introduced in the introduction of command mode command After the time, the function keys with the word "commonly used" are added after the commands, indicating the more commonly used vi commands, please read and remember them.

(1) I, a, o switch to Insert mode. [Super Common]

(2) To move the cursor vi, you can directly use the cursor keys on the keyboard to move up, down, left, and right, but the regular vi uses the lowercase English letters h, j, k, l to control the cursor to move left, down, up, and right by one cell.

Press Ctrl+B: The screen moves back one page. [Common] Press Ctrl+F: move the screen forward one page. [Commonly used] Press Ctrl+U: move the screen back half a page. Press Ctrl+D: Move the screen forward by half a page.

Press 0 (digital zero): Move the beginning of the article.

[Common] Press G: move to the end of the article.

[Commonly used] Press w: the cursor jumps to the beginning of the next word.

[Commonly used] Press e: the cursor jumps to the end of the next word.

Press b: the cursor returns to the beginning of the previous word.

Press $: move to the end of the line where the cursor is located.

[Common] Press ^: move to the first non-blank character on the line.

Press 0: Move to the beginning of the line.

[Commonly used] Press #: move to the #th position of the line, for example: 51, 121.

[commonly used]

 

(3) Delete text x: Each press deletes one character behind the cursor position. [Super Common]

         #x: For example, 6x means to delete the last 6 characters of the cursor position [common]

        X: X in large characters, each press deletes the previous character at the cursor position. #X: For example, 20X means to delete the first 20 characters of the cursor position. dd: delete the line where the cursor is located. [Super Common]

        #dd: For example, the 6dd table deletes 6 lines of text from the line where the cursor is located. [commonly used]

 

 

(4) Copy yw: Copy the characters from the cursor to the end of the word into the buffer. (I want to be the opposite of #x, #X) p: Paste the characters in the buffer to the cursor position (the command 'yw' and 'p must be used together). yy: Copy the line where the cursor is located. [Ultra Common] p: Copy a single line to where you want to paste it. (The command 'yy' and 'p' must be used together) #yy: For example: 6yy means to copy 6 lines of text from the line where the cursor is located. [Common] p: Copy multiple lines to the place you want to paste. (Instructions '#yy' and 'p' must be used together) "ayy: put the copied line into buffer a, vi provides the buffer function, which can store commonly used data in buffer "ap: paste the data in buffer a. "b3yy: Store three lines of data in buffer b. "b3p: Paste the data in buffer b

(5) Replace r: Replace the character where the cursor is: [Commonly used] R: Replace the character until you press Esc.

(6) Undo the last command u: If you operate an command by mistake, you can press u immediately to return to the previous operation. [Super Common] .: . Can repeat the last command.

(7) Change cw: Change the word where the cursor is to the end of the word $. c#w: For example, c3w means change 3 words.

(8) Jump to the specified line Ctrl+G: List the line number of the line where the cursor is located. #G: For example, 15G, which means move the cursor to the beginning of the 15th line of the article. [Commonly used] Introduction to commands in Last line mode Readers Before you want to use Last line mode, please remember to press the Esc key to confirm that you are already in Command mode, and then press the colon ":" or "/" or "?" One of the keys enters Last line mode.

1. List line numbers set nu: After typing "set nu", line numbers are listed before each line of the article.

2. Jump to a certain line of the article#: The pound sign represents a number, enter the number before the prompt symbol ":" in Last line mode, and then press Enter to jump to the line, such as: 15[Enter] will jump to the article Line 15. [commonly used]

3. Searching for a string/keyword: Press / first, then enter the word you want to search for, if the keyword you are looking for for the first time is not as good as you are, you can keep pressing n to search down to the keyword you want. ? Keyword: press first? , and then enter the word you want to search for, if the keyword you are looking for for the first time is not what you want, you can press n to search forward to the keyword you want.

4. Replace string 1, $s/string/replae/g: Entering "1, $s/string/replace/g" in last line mode will replace the full text string with the replace string, where 1, $s are It means that the search interval is from the beginning to the end of the article, and g means that all replacements do not need to be confirmed. %s/string/replace/c: It will also replace the full-text string string with the replace string. The difference from the above command is that %s and 1, $s have the same function, and c means to replace the previous Replacement must be confirmed again. 1,20s/string/replace/g: Replace the string between 1 and 20 lines with the replace string.

5. Save the file w: press w before the last line mode prompt symbol ":" to save the file. [Super common] #, # w filename: If you want to extract a certain paragraph of the article and save it as another file, you can use this command # to represent the line number, such as 30,50 w nice, the 30th ~ of the article you are editing 50 lines are saved as nice file.

6. Leave q: Press q to leave, sometimes if you cannot leave vi, you can use it with "!: Forced to leave vi, such as "q! ” qw: It is generally recommended to use it together with w when leaving, so that files can be saved when leaving. [Commonly used] VI command collection: Enter the vi command vi filename: open or create a new file, and place the cursor at the beginning of the first line of vi +n filename: open the file and place the cursor at the beginning of the nth line vi + filename: open the file and place the cursor at the beginning of the last line vi +/pattern filename: open the file and place the cursor at the first and pattern vi -r filename at the matched string: the system crashed when vi was last edited, restore filename vi filename....

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326655226&siteId=291194637