Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the replacement command in Vim

Shulou Source: shulou.com Published: 2022-06-02 00:11:52 10月27日 Update

Today, I would like to talk to you about what the replacement command in Vim is. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

Grammar

Vim can use the: substitute command to replace specified characters with other characters, usually in the abbreviated format of the command: s. Borrowing regular expressions can achieve richer replacement functions, and the basic syntax is:

: [range] s / source string / target string / [option]

Range and option can be left unfilled by default, and each field means:

Range: represents the scope of retrieval. The default is the current line search, 1jin10 represents lines 1 to 10,% represents the entire file, which is equivalent to 1 line, while $represents from the current line to the end of the file

Abbreviation for s:substitute, which stands for replacement

Option: represents the operation type. By default, only the first matching character is replaced by default. G (global) global replacement, c (comfirm) operation confirms that I (ignorecase) is not case-sensitive. These options can be used in combination.

For example, the following command will display the text that will be changed and ask for confirmation:

: 1 the with foo with foo (y/n/a/q/l/ E / Y)

At this point, you can choose the following actions:

Y Yes: perform this replacement n No: cancel this replacement an All: perform all replacements without asking Q Quit: exit without making any changes l Last: exit CTRL-E after replacing the current match point CTRL-Y tumble up one line practical case

1. Replace aa with bb in all strings containing aa that appear on the line of the cursor

: s/aa/bb/g

two。 Replace aa with bb in all strings containing aa that appear in the document

:% s/aa/bb/g

3. Replace aa with bb in all strings containing aa that appear from lines 12 to 23

: 12,23s/aa/bb/g

4. The full text begins with a # character, which is very useful when commenting in batches in Python

:% s / ^ / # /

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

:% s = * $=

6. Delete all blank lines, where g means to replace all strings that meet the requirements in the article, ^ indicates the beginning of the line, and $indicates the end of the line

: G / ^ $/ d after reading the above, do you have any further understanding of what the replacement command in Vim is? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Character string command representative content line file syntax retrieval utility cursor global full text function size field meaning location text document Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS vpn Docker NVidia