linux-Vi usage method

  
 

The vi editor is the standard editor for all Unix and Linux systems. Its power is not inferior to any of the latest text editors. Here is a brief introduction to its usage and a small number of instructions. Since the vi editor is identical for any version of Unix and Linux, you can learn more about it in any other place where you can introduce vi. Vi is also the most basic text editor in Linux. After learning it, you will be free in the Linux world.

1, the basic concept of vi basically vi can be divided into three states, command mode (command mode), insert mode (insert mode) and bottom line mode (last line mode), each mode The functions are divided as follows:

1) Command mode

Controls the movement of the screen cursor, deletes characters, words or lines, moves a certain section and enters Insert mode. Or to the last line mode.

2) Insert mode

Only in Insert mode, you can enter text. Press "ESC" to return to command line mode.

3) Last line mode

Save or exit vi, or set the editing environment, such as looking for strings, listing line numbers … …


But generally we use vi to simplify vi into two modes, which is to calculate the last line mode into the command mode.

2, the basic operation of vi a) Enter vi

After entering the vi and file name in the system prompt symbol, enter vi full screen editing screen:

$ vi myfile However, one thing to note is that after you enter vi, you are in "command mode", you can switch to "insert mode" to enter text. So after entering vi, don't tamper with it and switch to "Insert mode"!


b) Switch to Insert mode to edit the file

Click on the letter "i" in "command mode" to enter "Insert mode", you can start typing text at this time.


c) Insert Switch

You are currently in "Insert mode", you can only enter text all the time, if you find the wrong word ! If you want to use the cursor keys to move back and delete the word, you must first press the "ESC" button to go to "command mode" and delete the text.


d) Exit vi and save the file

Under "command mode", click the ":" colon key to enter "Last line mode" For example:

: w filename (enter "w filename" to save the article with the specified filename filename)

: wq (enter "wq", save and exit vi)

: q! (Enter q!, do not save the disk and force to exit vi)

3. Command mode function key 1). Insert mode

Press "i" to switch Enter the insert mode "insert mode", press "i" after entering the insert mode, the file is input from the current position of the cursor;

After pressing "a" to enter the insert mode, it is the next position from the current cursor position. The position starts to input text;

After pressing "o" to enter the insert mode, a new line is inserted, and the text is input from the beginning of the line.


2). Switch from insert mode to command line mode

Press the "ESC" button.


3). Move the cursor

vi can use the cursor on the keyboard to move up and down, left and right, but the regular vi is in lowercase English letter "h", " j", "k", "l" control the cursor left, down, up, and right respectively.

Press "ctrl" + "b": the screen moves to "after".

Press "ctrl" + "f": the screen moves to "front".

Press "ctrl" + "u": the screen moves to "after" to move half a page.

Press "ctrl" + "d": the screen moves to "front" to move half a page.

Press the number "0": Move to the beginning of the article.

Press "G": Move to the end of the article.

Press "$": Move to the line ending with " at the end of the cursor.

Press "^": move to the line where the cursor is located. "Line first"

Press "w": The cursor jumps to the beginning of the next word

e": The cursor jumps to the end of the next word

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

Press "#l": the cursor moves to the first line of the line #位置, such as: 5l, 56l.


4). Delete text

"x": Each time you press , delete the "back" character at the cursor position.

"#x": For example, "6x" means to delete the "back"6 characters at the cursor position.

"X": Uppercase X, each time you press , delete the "front" character at the cursor position.

"#X": For example, "20X" means to delete the "front"20 characters at the cursor position.

"dd": Delete the line where the cursor is.

"#dd": Delete #row from the line where the cursor is located


5). Copy

"yw": where the cursor is located The characters to the end of the word are copied into the buffer.

"#yw": Copy #words to buffer

"yy": Copy the line where the cursor is located to the buffer.

"#yy": For example, "6yy" means copying the line from the cursor where the cursor is "down"6 lines of text.

"p": Pastes the characters in the buffer to the cursor position. Note: All copy commands related to "y" must be combined with "p" to complete the copy and paste function.


6). Replace

"r": Replace the character at the cursor.

"R": Replace the character where the cursor is located until the "ESC" button is pressed.


7). Responding to the last operation

"u": If you execute a command by mistake, you can immediately press "u" to return to the previous operation. Pressing multiple times "u" can perform multiple replies.


8). Change

"cw": change the word at the end of the cursor to the end of the word

"c#w": for example, "c3w" means to change 3 words


9). Jump to the specified line

"ctrl" + "g" lists the line number of the line where the cursor is located.

"#G": For example, "15G" means moving the cursor to the beginning of the 15th line of the article.

4. Introduction to Commands in Last Line Mode Before using "last line mode", please remember to press "ESC" to confirm that you are in "command mode" and then press ":" colon. You can enter "last line mode".

A) List line numbers

"set nu": After entering "set nu", the line number will be listed in front of each line in the file.

B) Jump to a line in the file

"#": The "#" sign indicates a number, enter a number after the colon, and press the Enter key to jump to the OK, if you enter the number 15, and then press Enter, you will jump to the 15th line of the article.

Copyright © Windows knowledge All Rights Reserved