Three operating modes of Vi
Normal mode, also known as command mode
Insert mode
Command line (cmdline) mode
After entering Vi, the first thing you enter is the normal mode, and Vi waits Edit command input, that is, the letters entered at this time will be interpreted as commands.
In normal mode, enter the insert command i, append command a, open command o, etc. to enter insert mode. Any characters entered by the user in insert mode are saved as file content by Vi and displayed on the screen. During text entry, press the Esc key to return to normal mode.
In normal mode, use: to execute Ex commands, use ? and / to search, and use ! to call Shell commands. After the last line of commands is executed, Vi automatically returns to normal mode.
1. Operations in normal mode
Enter insert mode
i Insert before the cursor I Insert at the beginning of the cursor line
a Insert after the cursor A Insert at the end of the cursor line
o Insert a line below the line where the cursor is, and insert
O at the beginning of the line Insert a line on the current line, insert
cursor positioning
G at the beginning of the line, move to the beginning of the last line
nG, move to the beginning of the nth line
n+, move n lines down, and move the beginning of the line
n- up n Row, the beginning of the line
n$ Move down n lines (1 represents this line), the end of the line
0 The beginning of the line
$ The end of the line
^ The first letter of the line
h,j,k, l Move left, move down, move up, move right
H The beginning of the first line of the current screen
M The beginning of the middle line of the file displayed on the screen
L The beginning of the bottom line of the current screen
Replace and delete
rc Replace the character pointed by the cursor with c
nrc Replace the first n characters pointed by the cursor with c
x Delete the character at the cursor
nx Delete n characters starting from the cursor position to the right
dw If the cursor If you are in the middle of a word, delete from the position of the cursor to the end of the word (along with some spaces and tabs after the word)
ndw Delete n words starting from the word where the cursor is (the first word starts from the cursor) Start deleting the character where the cursor is located)
db credcred� \\\\\\\\\\\" Next n-1 lines
dG Delete the current line to the last line
d1G Delete the current line to the first line
d$ Delete the current character to the end of the line
d0 Delete the previous character to the beginning of the line
d, left The arrow deletes the previous character at the cursor
d, the right arrow deletes the character at the cursor
d, the up arrow deletes the current line and the previous line
d, the down arrow deletes the current line and the next line
Copy and pasteyy The current line is copied to the buffer
nyy The n lines currently starting are copied to the buffer
yG The line from the cursor to the last line is copied to the buffer
y1G The line from the cursor to the first line is copied to the buffer
y $ Copy the character from the cursor position to the end of the line to the buffer
y0 Copy the character before the cursor position to the beginning of the line and copy it to the buffer
p Write the contents of the buffer to the position of the cursor
Search string /str Move to the right to where there is str
?str Move to the left to where there is str
n Move in the same direction
N Move in the opposite direction
Undo and repeat U Cancel the previous one The impact of misoperation or inappropriate operation on the file
. Execute a command just completed before
ZQ Exit without saving
2. In command line mode The operation
jump line
:n Jump to the nth line String search and replace
:/str/ uleSKPT Move to the place where str exists
:/str/w file Write the line containing str to the file file / / /str/w file ) into the file file Write the content starting from str1 to the end of str2 to file In the file
:s/str1/str2/ Replace the first str1 found with str2
:s/str2/str2/g Replace all str1 found in the current line with str2
:n1,n2s/str1/ str2/g Replace all str1 found from line n1 to line n2 with str2
:1,.s/str1/str2/g Replace all str1 found from line 1 to the current line with str2
:., $s/str1/str2/g throughover in на на на на на на на на на на на на прание Replace all str1 from the 1st line to the last line with str2, and ask before replacing
:12,23s/^/#/ 12~23 lines plus #
*:%S = *$ == All the excess spaces at the end of the row delete : g/^s *$/d to delete all non -character (spaces and not included). Delete
:n1,n2 co n3 Copy line n1 to line n2 to the end of n3
File-related commands
:w cco remembered in in in inings in on fileâ€â€
:w file’ The currently edited content being written to the file
:n1,n2w file From line n1 to line n2, written to the file
Execute the Shell command
:!Cmd cred:n1,n2 w !Cmd Change the n1 line to n2 The line is used as the input of the Cmd command. If no line is specified, the entire file is used as the input of Cmd
:r !Cmd Write the result of the command execution to the next line of the line where the cursor is (the command cannot be an alias)
Set Vi Environment
:set autoindent Auto indent
:set noautoindent Cancel automatic indentation
:set number Display line numbers when editing files
:set nonumber wealth in noautoindent 》 location The row and column positions