The vi editor has three modes: Command mode, input mode, and last line mode. It is very important to master these three modes:
Command mode: After vi is started, the command mode is entered by default. From this mode, you can use the command to switch to the other two modes.
In any mode, you only need to press the [Esc] key to return to the command mode. Enter the subtitle "I" in command mode to enter the vi input mode.
Files.
Input mode: In this mode, you can edit, modify, input, and so on. In the last line of the editor, a "-- INSERT
-- "Indicates that vi enters the input mode. When we complete the modification and input operations, we need to save the file. In this case, we need to first return
Command mode, which is saved in the last row mode.
Last line mode: Enter ":" In command mode to enter this mode. There are many useful commands in last line mode.
1. Access the vi command
Vi filename: open or create a file and place the cursor at the beginning of the first line
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.
2. move the cursor command
H or Backspace: move the cursor one character to the left
L or space: move the cursor one character to the right
K or Ctrl + p: move the cursor up a row
J or Ctrl + n or Enter: move the cursor down a row
W or W: move one word to the beginning of the word to the right of the cursor.
B or B: move one word left to the beginning of the word
E or E: move one word to the end of the word to the right of the cursor.
): 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
NG: move the cursor to the beginning of line n
N +: move the cursor down n rows
N-: Move n rows above the cursor
N $: move the cursor to the end of line n
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
0: (Note the number is zero) move the cursor to the beginning of the current row
$: Move the cursor to the end of the current row
3. Screen tumble commands
Ctrl + u: half screen at the beginning of the file
Ctrl + d: Flip the half screen to the end of the file
Ctrl + f: Flip the screen to the end of the file
Ctrl + B; open a screen at the beginning of the file
Nz: Rolls row n to the top of the screen. If n is not specified, the current row is rolled to the top of the screen.
4. Insert text commands (text input mode)
I: Before the cursor
I: at the beginning of the current row
A: After the cursor
A: At the end of the current row
O: open a new row under the current row
O: open a new row above the current row.
R: Replace the current character
R: Replace the current and subsequent characters until you Press ESC.
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
Ncw or nCW: modify a specified number of characters
NCC: modifies a specified number of rows.
5. delete command
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
Ndd: Delete the current row and the next n-1 row
Dd: delete a row
X or X: delete a character. x deletes the character after the cursor, and X deletes the character before the cursor.
Ctrl + u: delete text entered in input mode
6. copy operation
Run the yy command to copy the entire line to the vi buffer.
Yw copies the content from the current cursor position to the end of the word to the vi cache, which is equivalent to copying a word.
Y $ copy the cursor to the cache area at the end of the row
Y ^ copy the cursor to the cache area from the beginning of the line
# Yy. For example, 5yy means copying 5 rows.
# Yw: for example, 2yw means copying two words.
If you want to copy the content between Row m and row n, you can enter m in the last row mode. For example, ny: 3, 5y copies the content from row 3 to row 5 to the cache area.
Paste the content in the buffer, and use p
7. Cancel the operation
U command to cancel the last operation, you can use multiple times to restore the original operation
U cancel all operations
Ctrl + R can be used to restore operations using the u command
8. search and replace commands
The search and replacement functions of vi are mainly completed in the last line mode:
Top-down search
/The character seek to be searched./indicates that the search starts from the position where the cursor is located, for example:/work
Bottom-up search
? For example,/work
Replace
: S/old/new replace the old
: S/old/new/g replace all existing old values in the row with new.
: #,# S/old/new/g replace the old value from row # To row # With new
: % S/old/new/g replace the old
9. option settings
: Set nu display row number
: Set nonu hidden row number
: Set showmode: displays the current operation mode (if vedit is used, it is automatically enabled)
: Set noshowmode Off Mode Display
: Set: Display All vi Variables
: Set all: displays all possible vi variables and their current values.
All: lists all options.
Term: Set the terminal type
Ignorance: Case Insensitive in search
List: displays the stop table (Ctrl + I) and end mark ($)
Number: displays the row number.
Report: displays the number of changes made by line-oriented commands.
Terse: displays brief warning information
Warn: NO write information is displayed if the current file is not saved when it is transferred to another file.
Nomagic: allows you to use special characters without "\" in search mode.
Nowrapscan: Prohibit vi from searching at both ends of the file and starting from the other end.
Mesg: Allows vi to display the information that other users write to their terminal using write.
10. Last-line command (last-line mode)
: 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
: W: Save the current file
: E filename: open the file filename and edit it.
: X: Save the current file and exit.
: Q: Exit vi.
: Q! : Do not save the file and exit vi
:! Command: execute shell command
: N1, n2 w! Command: The content from line n1 to line n2 in the file is used as the command input and executed. If n1, n2 is not specified
File Content as command input
: R! Command: place the output result of the command to the current line.
11. Restore Files
When editing a file, vi will generate a temporary file that starts with. And ends with. swp. Exit the file normally automatically
Delete: if the file is accidentally exited, such as a sudden power failure, the file will not be deleted. You can select the command to process it the next time you edit the file:
O read-only access without changing the File Content
E. Continue to edit the file and do not restore the content saved in the. swp file.
R restores the content of the file not saved after the last Edit
Q: Exit vi
D. Delete the. swp file.
Or use the vi-r file name to restore unsaved content.
12. Register operation
"? Nyy: Save the content of the current row and Its n rows to the register? , Where? It is a letter, and n is a number.
"? Nyw: Save the current row and Its n characters to the register? , Where? It is a letter, and n is a number.
"? Nyl: Save the current row and Its n characters to the register? , Where? It is a letter, and n is a number.
"? P: retrieve the register? And place it at the cursor position. Here? It can be a letter or a number.
Ndd: delete n lines of text from the current row and its bottom, and put the deleted content in the delete Register No. 1.
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/love_tu/archive/2009/08/07/4421389.aspx