Linux Command VI/Vim

Source: Internet
Author: User

I. 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:
A) Command Line 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.
B) insert mode)
You can enter text only in insert mode. Press ESC to return to command line mode.
C) Baseline 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 ).

Ii. 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 need to switch to insert 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 tamper with the mouse, switch to "insert mode!

$ Vi + N filename: open the file and place the cursor at the beginning of line N.
$ 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 string matching pattern.
$ Vi-r filename: a system crash occurred when VI was being edited last time, restoring filename.
$ VI filename... filename: open multiple files and edit them one by one.

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 switchover
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"ArticleSave 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
A). 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;

I: Start to input the file at the beginning of the current line;

After you press "a" to enter the insert mode, the text is entered starting from the next position where the cursor is currently located;

A: Start the input file at the end of the current row;

After you press "O" to enter the insert mode, a new row is inserted and text is entered from the beginning of the line.

O: open a new row above the current row.

S: Starting from the current cursor position, replace the specified number of characters with the input text
S: delete a specified number of rows and replace them with the input text

B) switch from insert mode to Command Line Mode
Press ESC.
C) 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.

Space: move the cursor one character to the right
Backspace: move the cursor one character to the left

Enter: move the cursor down a row
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": move the cursor to the beginning of the current row.

Press "$": move the cursor to the end of the current row.

N $: move the cursor to the end of line N

GG: Move to the first line of this case, equivalent to 1G!

Press g to move to the end of the article.

NG: move the cursor to the beginning of line N

CTRL + G lists the row numbers of the cursor.

Press "^" to move to "non-null characters at the beginning of the line" in the row where the cursor is located"
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

): Move the cursor to the end of the sentence.
(: Move the cursor to the beginning of the sentence.
}: Move the cursor to the beginning of the paragraph
{: Move the cursor to the end of the paragraph
N +: move the cursor down n rows (equivalent to n <enter>)
N-: Move n rows above the cursor
H: move the cursor to the top line of the screen
M: move the cursor to the middle line of the screen
L: move the cursor to the last line of the screen

Press # l to move the cursor to the # position of the row, such as 5l and 56l.
D). delete text
"X": each time you press the button, the "Next" character of the cursor is deleted.

"X": uppercase X. Each time you press it, the "front" character of the cursor is deleted.

"# X": for example, "6x" indicates deleting the "Next" 6 Characters of the cursor position.
"# 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

Ndw or ndw: n-1 characters starting at and following the cursor
Do: Delete to the beginning of a row
D $: Delete to the end of the row
CTRL + u: delete text entered in input mode

E). 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.
F). Replace
"R": Replace the character at the cursor position.
"R": Replace the character wherever the cursor goes until you press the "ESC" key.
: 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 vbird between 100 and 200 and replace it with vbird: ": 100,200 S/vbird/g 』.

: 1, $ S/word1/word2/G: Find the word1 string from the first line to the last line and replace it with word2!

: 1, $ S/word1/word2/GC: Find the word1 string from the first line to the last line and replace it with word2! In addition, the prompt character is displayed before replacement to the user to confirm (confirm) whether to replace!

G). 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.
H). Change
"CW": change the word at the cursor to the ending point.
"C # W": for example, "c3w" indicates that three words are changed.

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

「: Set Nonu 」: opposite to set Nu, In order to cancel row renewal!

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.

: W [filename]: Save the specified item into another example (similar to saving a new example)

: R [filename]: The data in another case is imported into the response. That is to say, add the "FILENAME" content to the back of the game tag line.
E) Leave Vi
Q: Press Q to exit. If you cannot exit Vi, you can follow Q with 「!」 Force exit VI.
"WQ": it is generally recommended that you use "W" when you exit, so that you can save files when you exit.

: N1, N2 Co N3: copy the content from line N1 to line N2 to line N3.
: N1, N2 M N3: Move the content from line N1 to line N2 to line N3
: N1, N2 D: Delete content from line N1 to line N2

: E filename: open the file filename and edit it.

: X: Save the current file and exit.

:! Command: the result of Executing command in command column mode when VI is temporarily left! For example 『:! Ls/Home> to view the file information output by ls under/home in Vi!

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