Replace [zz] globally in VIM & replace the character with the carriage return

Source: Internet
Author: User

From http://blog.csdn.net/shuangde800

 

This article takes notes when you learn how to use the VI Editor, prepared by Lamb & Robbins. Content:Basic global replacement confirm replacement context-related replacement This article stipulates:$ + Command indicates that the command is in normal bash command line: + command indicates that in VIM ex (command) mode, press "Ctrl" + ":" to enter bash ------------------------------------------------------------------------------------------------ In global replacement, the ex editor checks each line in the file in the specified character mode. Locate the pattern position in all rows. Ex replaces the pattern with the new string. Global replacement actually uses two ex commands: : G(Global) and : S(Switch replacement ). The syntax of the replace command is as follows: : S/old/new/This will change the first appearance of the current medium mode old to new. /(Diagonal Bar) is the delimiter between different parts of the command (when the Diagonal Bar is the last character of this line, do not write) the following form of replacement command: : S/old/new/gChange the appearance of the old row to new instead of the first old one. : The s command allows replacement strings to be followed by an option. The G in the above syntax represents the global situation (the G option affects each mode in a row and does not confuse it with the G command that affects all lines in the file) by adding the address prefix before the: s command, you can extend its range to multiple lines. For example, the following command changes the appearance of old from 50 to 100 rows to new: 50,100 S/old/new/gThe following command changes every appearance of the old in the entire file to new: : 1, $ S/old/new/gWe know that % is equivalent to 1, $, so the upstream command can also be written as follows: : % S/old/new/g Make sure to replace the search replacement command. Sometimes, an error occurs and you can get unwanted results. So it is wise to be careful and confirm what needs to be modified in the file by adding C(Confirm is used for confirmation), a prompt and confirmation will be prompted before replacing each old one: : 1, 30 s/old/new/GCThe following message is displayed: Replace with hehe (y/N/A/Q/L/^ e/^ y )? Y replace, n do not replace, a replace all, Q discard, l Replace the first one and enter the insert mode, ^ e and ^ y indicate that you use Ctrl + E or Ctrl + Y to scroll the screen. In addition to replacing one word (or phrase) with another, context-related replacement also has a slightly complex global replacement syntax. These syntaxes can be used to search for a mode. Once a row with a mode is found, strings with different modes can be replaced. We call this replace context-related replace. The syntax format is as follows: : G/pattern/S/old/new/gReplace all old with new in the line containing pattern. The first G indicates the Command executed on all lines of the file. Pattern identifies the line to be replaced. On the rows that contain pattern, ex will replace (s) with new. The last G indicates that all replace is performed on this row. If the pattern used for search is the same as the pattern to be modified, you do not need to repeat it: : G/string/S // new/gEquivalent : G/string/S/string/New/gNote: : G/string/S // new/gIt is also equivalent : % S/string/New/gYou can use the second method to enter less characters and press Enter. In vim, press Ctrl + V to obtain ^. we will get ^ m, which is the carriage return statement. So the statement to replace all STR in the entire file with str carriage return is:
: 1,$ s/str/str^M/g

^ M input using the above method

1, $ indicates from 1 to the last line, S is switch, G is global

Replace [zz] globally in VIM & replace the character with the carriage return

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.