Organize vi commands in CentOS

Source: Internet
Author: User

Organize vi commands in CentOS

Vi has three modes: "General mode", "edit mode", and "command line command mode ".

L normal mode: when vi processes a file, it is normal as soon as it enters the file. In this mode, you can use the "up/down" key to move the cursor. You can use "delete character" or "Delete whole line" to process the file content, you can also use "copy and paste" to process file data.

L editing mode: In normal mode, you can delete, copy, and paste files, but cannot edit files, the editing mode will not be available until you press the letters "I, I, o, O, a, A, r, and R. Note: in Linux, when you press the above letter, "INSERT or REPLACE" will appear in the lower left corner of the screen before you can enter characters in the file. Back to normal mode, Press ESC to exit the editing mode.

L command line mode: In normal mode, enter ":" or ?" You can move the cursor to the bottom row. In this mode, you can search for data, in addition, operations such as reading, saving disks, deleting a large number of characters, leaving vi, and displaying row numbers are all implemented in this mode.

1. Use vi to enter the General Mode

[Root @ linux] # vi test.txt

Enter "vi + file name" to enter the General vi Mode.

2. Press I to enter the editing mode.

In edit mode, INSERT appears at the bottom left of the screen.

3. Press ESC to return to normal mode.

Press the [ESC] key and you will immediately find that the-INSERT-in the lower left corner of the screen is missing.

4. Exit the vi editor.

In normal mode, press ": wq" to save and exit vi, and press ": q" to save and edit and exit vi. Note: When the "read-only" class is prompted when exiting, you can add "!" after the command. Indicates the meaning of "force.

Common commands

I. move the cursor

1) h or move a character to the left

2) j or downward direction (cursor) → move the cursor down a character

3) k or forward (forward) → move the cursor up a character

4) l or forward (→) to the right → move a character to the right of the cursor

5) [Ctrl] + [f] → move a Page Down, which is equivalent to a [Page Down] button.

6) [Ctrl] + [B] → move a Page Up, which is equivalent to a [Page Up] button.

7) n <space> → n indicates a number 』. Press the number and then press the blank cursor. The cursor will move the n characters of this line to the right.

8) 0 → number "0": Move to the top character of the line

9) $ → move to the last area of the line

10) G → move to the last row of the file

11) nG → n is a number. The Nth row to be moved to this file (can be used with: set nu)

12) gg → move to the first line of the file, equivalent to 1G

13) n <Enter> → n is a number. Move the cursor down n rows

Ii. search and replace

1)/word → search for a string named word under the cursor. To search for the eric4ever string, enter/eric4ever.

2 )? Word → search for a string named word over the cursor

3) n → "repeat the previous search action 』

4) N → opposite to n, perform the previous search action for "reverse"

5): n1, n2s/word1/word2/g → n1 and n2 are numbers. Search for the word1 string between line n1 and line n2 and replace it with word2. For example, if you search for eric4ever between lines 1 and 9 and replace it with ERIC4EVER: ": s/eric4ever/ERIC4EVER/g 』

6): 1, $ s/word1/word2/g → search for the word1 string from the first row to the last row, and replace the string with word2.

7): 1, $ s/word1/word2/gc → search for the word1 string from the first row to the last row, and replace the character window with word2. Whether to confirm replacement in the prompt dialog box before replacement

Delete, copy, and paste

1) x, X → in a single line of text, x is a forward trim in addition to a character (equivalent to a [del] button), X is a forward trim in addition to a character (equivalent to a [backspace] button)

2) dd → remove the entire line of the cursor

3) ndd → n is a number. Remove the n-column down where the cursor is located

4) yy → copy the row where the cursor is located

5) nyy → n is a number. Copy the down n column of the cursor

6) p, P → p: paste the copied content on the next line of the cursor, and P is pasted on the previous line of the cursor.

7) u → restore the previous action

8) [Ctrl] + r → redo the previous action

9). → decimal point, meaning to repeat the previous action

★Edit mode

1) I, I → insert: insert the entered text at the current cursor location. The existing text will be backward. Where, I is "insert from current cursor location 』, I is "starting to insert the first non-blank character in the current row 』

2) a, A → a is "insert from the next character of the current cursor", and A is "insert from the last character of the row where the cursor is located 』

3) o, O → o is "Insert a new row in the next row where the current cursor is located", O is "Insert a new row in the previous row where the current cursor is located 』

4) r, R → r is the character that replaces the cursor, and R is the text that replaces the cursor until you Press ESC 』

5) Esc → exit the editing mode and return to normal mode

★Last line command mode

1): w → write the edited file to the hard disk

2): q → exit vi

3): wq → save and exit. If it is: wq! It is forcibly saved as exit

4): set nu → display the row number

5): set nonu → undisplay the row number

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: 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.