General replacement
S/old/new--- Replace the first matching item of the current row
S/old/new/g---- Replace all the matching items in the current row
Number1, number2-s/old/new/g---- Replace all the matching items from the number1 row to the number2 row
1, $ S/old/new/g = % S/old/new/g----- Replace all matching items of the entire file
Confirm and replace
Add a C (confirm) after the replacement command, for example, 1, 30 s/his/The/GC. Each time you find the matching item, you will be prompted whether to replace it. Enter y, enter to confirm the replacement, and enter to skip the replacement.
Replace context-sensitive with g/pattern/S/old/new/g
The first G indicates that pattern needs to be searched from all rows, and the following G indicates that all matching items of the current row are replaced.
If pattern and old are the same, old can be omitted. This command is equivalent to % S/old/new/g.
Regular Expression matching rules
.---- Match all characters except line breaks
*---- Match 0 or multiple characters
^---- Match the beginning of a row
$---- Match the end of a row
\---- Anti-Italian character, indicating that the subsequent character is a regular character.
\(\)--- Placeholder. A maximum of 9 placeholders can exist in a row. For example, if \ (that \) or \ (this \) \ (that \) is set to 1, use that to replace \ 1, \ (this \) it is called by \ 2. For example, if you replace that or this with this or that, you can use the command % S/\ (that \) or \ (this \)/\ 2 or \ 1.
\ </>---- Match a word that starts or ends with a character. For example, \ <AC matches a word that starts with AC, and AC/> matches a word that ends with AC.
~---- Match the previous Regular Expression in the search. For example, if you want to start searching for/The, you can use /~ N to replace/then
Vim uses ---- global replacement