Global substitution of Vim

Source: Internet
Author: User

From

This article is in the notes that were written when you learned to use VI Editor, Lamb & Robbins.

The content of this article:

Basic Global substitution

Confirm Replacement

Context-sensitive substitution

This article has agreed:

The $ + command is indicated under normal bash command line

: + command indicates the EX (command) mode in Vim, press "Ctrl" + ":" To enter

------------------------------------------------------------------------------------------------

Basic syntax for replace command

In global substitution, the ex editor checks each line in the file using the specified character pattern. The position of the pattern is found in all rows, and ex uses the new string to replace the pattern.

The global substitution really uses two ex commands : G(global globally) and : s(switch substitution).

The syntax for the Replace command is as follows:

: S/old/new/

This will change the first occurrence of the current mode old to new. /(diagonal bar) is the delimiter between different parts of the command (not written when the diagonal bar is the last character of the line)

Here is the alternate command for this form:

: s/old/new/g

Change each occurrence of the current line old to new, not just the first old of the row. : The s command allows the replacement string to be followed with an option, g in the above syntax represents global (g option affects each pattern in a row, do not confuse it with the: G command that affects all lines in the file)

By adding an address prefix to the: s command, you can extend its scope to multiple lines. For example, the following command changes each occurrence of an old of 50 rows to 100 rows to new:

:-s/old/new/g

The following command changes each occurrence of an old in the entire file to new:

: 1, $s/old/new/g

We know that% is equivalent to 1,$, so the upstream command can also be written like this:

:% s/old/new/g

Confirm Replacement

Using the search Replace command sometimes makes mistakes and gets unwanted results. So it's a good idea to be careful and confirm what you need to modify in the file.

Add C (confirm for confirmation) at the end of the replacement command and prompt and confirm before replacing each old:

: 1, S/OLD/NEW/GC

The prompt replace with hehe (y/n/a/q/l/^e/^y) will appear?

Y replaces, N does not replace, a replaces all, Q discards, L replaces the first one and goes into insert mode, ^e and ^y are prompts you to use Ctrl+e or ctrl+y to scroll the screen.

Context-sensitive substitution

In addition to replacing one word (or phrase) directly with another, there is a slightly more complex global substitution syntax. These grammars can be searched for a pattern, and once a row with a pattern is found, it can be replaced with a different string of patterns, which we call a context-sensitive substitution.

The syntax format is as follows:

: g/pattern/s/old/new/g will replace all old in the line containing the pattern with the new

The first G represents a command that executes on all the lines of a file, and patterns pattern identifies the row to be replaced. On lines that contain pattern patterns, ex will replace old (s) with new. The final g indicates that all substitutions are made on the line.

If the pattern used to search is the same as the pattern you want to modify, you do not have to repeat it:

: g/string/s//new/g equivalent to : g/string/s/string/new/g

Also pay attention to:

: g/string/s//new/g is also equivalent to :% s/string/new/g

You can lose a few characters in the second way

Global substitution of Vim

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.