Vim Text Substitution command

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_94a93cff0101b5gv.html


To replace text in VIM:
1. Replace the contents of the current line:: s/from/to/(S-substitude): s/from/to/: Replaces the first from in the current row with the to.        If the current row contains more than one from, only the first of them will be replaced.        : s/from/to/g: Replaces all from in the current row with to. : s/from/to/gc: Replace all from in the current row with to, but ask the user to confirm the action before each replacement.
Note: The From and to can be any string, where from can also be a regular expression.
2. Replace the contents of a line:: 33s/from/to/g:. s/from/to/g: Replace operation on current line.        : 33s/from/to/g: Replace operation on line 33rd. : $s/from/to/g: replace operation on the last line.
3. Replace the contents of some rows:: 10,20s/from/to/g: 10,20s/from/to/g: Replace the contents of lines 10th through 20th.        : 1, $s/from/to/g: replaces the contents of the first line to the last line (that is, all text).        : 1,.s/from/to/g: Replaces the contents of the first line to the current line.       :., $s/from/to/g: replaces the contents of the current line to the last row.                         : ' A, ' bs/from/to/g: Replace the line between Mark A and B (containing the rows of A and B). Where A and B are the marks previously made with the M command.
4. Replace the contents of all rows::%s/from/to/g:%s/from/to/g: Replace the contents of all rows.
    5.  replacement command complete form:   :[range]s/from/to/[flags]        5.1s/from/to/          Replaces the string from the from specified to the specified string, from can be a regular expression.        5.2[range]           has the following representations:           Do not write range :  default to the line where the cursor is located.           .       : The line where the cursor is located.           1       : First line of  .           $       :   last line.           33      &nbsp: 33rd line of  .           ' a       :  mark a line (previously marked with MA).           .+1      :  the line below where the current cursor is located.           $-1      :  the penultimate line. (This shows that we can add a value to a line                      get the opposite line.           22,33    : line 22nd to 33rd of  .           1,$      :  line 1th to the last line.           1,.      :  line 1th to current line.           .,$      :  The current line to the last line.           ' A, ' b    : The line where Mark A is located is the line to mark B.
%: All rows (equivalent to 1,$).
? Chapter? : Search up from the current position to find the row of the first chapter.           (where chapter can be any string or regular expression.) /chapter/: Search down from the current position, find the line where the first chapter is located. (where chapter can be any string or regular expression.)
Note that all of the above presentation methods for range can be set relative offsets through the +,-action.
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: Request user confirmation before replacing. E: Ignore errors in the execution process.
Note: All flags above can be grouped together, for example, the GC represents a replacement for all occurrences within the specified range, and the user is asked to confirm each time it is replaced.

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.