Search command:
/Character to be
searched n, forward search - next
N, reverse search - previous
? Search character
up
Find and replace:
%s/source string/new string/g replace from the beginning to the end (full replacement)
n,%s/source string/new string/g start from the nth line to replace all
There are many exit commands of Vi , and the commonly used ones are as follows:
- :w saves the file without exiting vi
- :w file save as file file, do not exit vi
- :w! Force save, do not exit vi
- q: do not save the file, exit vi
- :q! Do not save the file, force quit vi
- :wq save the file and exit vi
- :wq! Force save the file and exit vi
- :x write to file and exit
- :e! Abandon all changes and start editing from the last time the file was saved
About the difference between the x command and the wq command:
wq force write to file and exit. Force a write even if the file is not modified, and update the file's modification time.
x writes the file and exits. Write only when the file is modified, and update the file modification time, otherwise the file modification time will not be updated.
'u' : undo the last edit operation
'ctrl + r' : restore, that is, go back to the previous command
'U' : line undo, undo all operations on the previous edit line
Use the u and CTRL+R commands to revert to any edited state
Cursor movement:
H, J, K, L same direction keys left, down, right, up
- move the cursor up one line
+/Enter cursor down one line
backspace/space cursor left/right one character
Ctrl + F Scroll down one screen
Ctrl + B Scroll up one screen
Ctrl + D scroll down half a screen
Ctrl + U scroll up half screen
Ctrl + E Move the file content in the editing window up one line as a whole
Ctrl + Y Move the file content in the editing window down one line as a whole
w Moves the cursor one word to the right. The cursor stays at the beginning of the next word.
W Moves the cursor one word to the right. The cursor stays at the beginning of the next word (even if there is a punctuation mark between the words).
b Moves the cursor one word to the left. The cursor stays at the beginning of the next word.
B Moves the cursor one word to the left. The cursor stays at the beginning of the next word (even if there is a punctuation mark between the words).
e Move the cursor to the last character of the current word (or the next word).
E Same as above, except that the space character is used as the word separator.
^ Move the cursor to the beginning of the current line, that is, the position of the first non-blank character of the current line
0 (zero) Same as above
$ Moves the cursor to the end of the current line, that is, the last character position of the current line.
H Moves the cursor to the beginning of the first line at the top of the editing window.
M Moves the cursor to the beginning of the line in the middle of the editing window.
L Moves the cursor to the beginning of the last line at the bottom of the editing window.