Replacement in vim

Vim can use the :substitute command in the last line mode to replace the specified string with other target strings.

Usually we can use the abbreviated format of the command: s to perform vim string replacement operations

1. The syntax of Vim replace string command

The basic syntax of the Vim replace string command is: [range]s/target string/replacement string/[option], in which the range and option fields can be left blank by default.

The following describes the meaning of each variable of the VIM replacement string:

Range: indicates the search range, the default indicates the current line;

  The range field value is 1, 10 means from the 1st to the 10th row;

  % Means the entire file (equivalent to 1, $);

  And ., $ represents from the current line to the end of this file

 

s: Shorthand for substitute, which means to perform replacement string operations;

option: indicates the type of operation. By default, only the first matched character is replaced;

The option field value g (global) means global replacement;

c (comfirm) indicates that confirmation is required during operation;

i (ignorecase) means case insensitive;

These options of vim replacement string can be used in combination

Two, Vim replacement command examples

Vim replaces the command globally and requires confirmation of the replacement string command

Replace all Vim strings with vim strings

1, $s/Vim/vim/gc will prompt "replace with foo(y/n/a/q/l/^E/^Y)?", asking whether to confirm the execution

The meaning of the operation to be selected includes:

y: Confirm that this replacement will replace all Vim with vim;

n: Cancel the operation of this original Vim replacement command;

a: Perform all replacement string operations this time and no longer ask;

q: Exit the current vim string replacement operation without making any changes;

l: Exit after replacing the current matching point (last)

Ctrl + E: Scroll up one line

Ctrl + Y: scroll down one line

2 Replace line with lines in all strings containing line appearing on the line where the cursor is located

:s/line/lines/g means to replace the line of the current line where the cursor is with lines globally

3 Replace lines in all strings containing lines appearing in lines 2 to 10 with lines

:2,10s/line/lines/g means to replace the lines of 2~3 lines with lines globally

4. Add // characters at the beginning of the full text, very useful for batch comments

:%s/^/\/\// means to replace and insert // at the beginning of the full text range. Note that you need to escape / in Vim before you can replace it

5. Delete all extra spaces at the end of the line

:%s= *$== means that one or more spaces at the end of the line are replaced globally. For more regular expression descriptions, please refer to Vim regular expressions



Author: i Andrew Lam Cheng
link: https: //www.jianshu.com/p/0175bed4092d
Source: Jane books
are copyrighted by the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/qq_42533216/article/details/112425805