to replace text in VIM:1. Replace the contents of the current line:: s/ from/to/(S is substitude): s/ from/to/: Replaces the first from, in the current row, with a to. If the current row contains more than one from, only the first one is replaced. : s/ from/to/g: Replace all from in the current line with to. : s/ from/to/GC: Replaces all from in the current row with to, but asks the user to confirm the action before each replacement. Note: Both from and to can be any string, where the from can also be a regular expression. 2. Replace the contents of a line:: 33s/ from/to/g:. s/ from/to/G: The replacement operation is performed on the current line. : 33s/ from/to/G: The replacement operation is performed on line 33rd. : $s/ from/to/g: Replace on the last line. 3. Replace the contents of some lines:: 10,20s/ from/to/g:10,20s/ from/to/g: Replace the contents of lines 10th through 20th. :1, $s/ from/to/g: Replace the contents of the first line to the last line (that is, the entire text). :1,.s/ from/to/g: Replace the contents of the first line to the current line. :., $s/ from/to/g: Replace the contents of the current row to the last row. :'A,'bs/ from/to/g: Replace the line between the mark A and B (with the row where A and b are located). Where A and B are tokens that were previously made with the M command. 4. Replace the contents of all lines::%s/ from/to/g:%s/ from/to/g: Replace the contents of all rows. 5. The complete form of the replacement command:: [range]s/ from/to/[Flags]5.1 s/ from/to/replaces the from specified string with the string specified by to, and from can be a regular expression. 5.2[Range] has some of the following representations: Do not write range: The default is the line where the cursor is located ... : The line where the cursor is located. 1: First line. $: Last line. 33: Line 33rd. 'A: Mark the line where A is located (previously marked with MA). . +1: The line under which the current cursor is located. $-1: The second line to the bottom. (This shows that we can add a value to a row to get a relative line). 22,33: Line 22nd to 33rd. 1, $: line 1th to the last row. 1,. : line 1th to Current line ..., $: Current line to last row. 'A,'B: Mark the line where a is located to the row where Mark B is located. %: All lines (with 1, $ equivalent). Chapter? : Searches up from the current position, and finds the row where the first chapter is located. (where chapter can be any string or regular expression.) /chapter/: Searches down from the current position, and finds the row where the first chapter is located. (where chapter can be any string or regular expression.) Note that all of the above representations for range can be+,-action to set the relative offset. 5.3[Flags] The flags available here are: none: Replaces only the first occurrence within the specified range. G: Replaces all occurrences within the specified range. C: Ask the user to confirm before replacing. E: Ignore errors in the execution process. Note: All flags above can be used together, such as GC to replace all occurrences within a specified range, and to be confirmed by the user before each substitution.
Vim Text Substitution command