Summary of common search, replacement, and deletion modes in VIM

Source: Internet
Author: User
Tags repetition

1. Simple replacement expression

Remove spaces at the end of all rows:% S/\ s \ + $ //

Remove all blank rows:% S/\ (\ s * \ n \) \ +/\ r/

Remove all "//" Annotations:% S! \ S *//.*!!

Remove all "/*... */" Annotations:% S! \ S */\ * \ _. \ {-} \ */\ s *! ! G

Press ENTER ^ m: % S/R // G for DOS deletion.
: % S = * $ = Delete the blank space at the end of the row:
: % S/^ (. *) N1/1 $/delete duplicate rows:
: % S/^. {-} PDF/newexample/only deletes the first PDF:
: % S/<! -- _. {-} --> // Delete the multi-line comment (comment? Why do we say "again ?)
: G/S * ^ $/d delete all empty rows: Is this easy to use? Has anyone used any other methods?
: G! /^ DD/d Delete the row without the string 'dd'
: V/^ DD/d same as above (translated as V = G !, Is not matched !)
: G/str1/,/str2/d delete all the rows from the first entry containing str1 to the first entry containing str2.
: V/./,/./-1 Join compressed empty rows
: G/^ $/,/./-J compressed empty rows

 

2. Simple DELETE command
Ndw or ndw deletes n-1 characters starting at and following the cursor.
Delete D0 to the beginning of the row.
D $ Delete to the end of the row.
NDD deletes the current row and the next n-1 row.
Delete a character from X or X.
CTRL + u Delete the text entered in the input mode.
^ R restore u
J. Merge the next row to the end of the current row
V select a row
^ V press ^ V to select a rectangle.
Aw select word
IW internal words (no space)
As Select sentence
Is Select sentence (no space)
AP selection Section
IP selection section (no space)
D. Delete it to the end of the row.
X, Y Delete and copy contain highlight Area
DL deletes the current character (same as the X command)
D0 is deleted to the starting position of a row.
D ^ Delete the first character (excluding spaces or tab characters) to a row)
DW is deleted to the end of a word.
D3w is deleted to the end of the third word.
DB is deleted to the start position of a word.
DW is deleted to the end of a word with a space as the separator.
DB is deleted to the start position of a word with space as the separator.
D7b is deleted to the start position of the first seven words with spaces as separators.
D) Delete to the end of a statement
D4) to the end of the fourth statement.
D (delete to the start position of a statement)
D) delete it to the end of a paragraph.
D {Delete to the beginning of a paragraph
D7 {Delete the first 7th paragraph positions before the start position of the current paragraph
Dd deletes the current row
D/Text: Delete the position specified by "text" from the text,
Forward until the content of the next occurrence (but not including the word)
DFC deletes the position where the character "C" appears from the text and continues until the next occurrence of the character (including the character ).
DTC deletes the content from the current row until the next character "C" appears
D. Delete it to the end of a row.
D $ Delete to the end of a row
5dd Delete the contents of the five rows starting from the current row
Delete DL content until the last line on the screen
DH Delete until the first line of content on the screen
Delete content from the DG until the end of the work Cache
D1g Delete content that begins in the work Cache

: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line

: S/str1/str2/g replace all the str1 strings in the row with str2

:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2

: 1, $ S/str1/str2/g replace str1 with string str2

: G/str1/S // str2/g functions are the same as above

From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only search

String is replaced for the first time. G is placed at the beginning of the command to replace all rows containing the search string in the body.

Returns a string that can be searched to reach the specified row. If you want to perform a forward search, place the string to be searched in two

/"; If you want reverse search, place the string in Two"?" . For example:

:/Str/forward search, move the cursor to the next row containing the string Str

:? Str? Reverse search: move the cursor to the previous row containing the string Str

:/Str/W file: Forward search, and write the first row containing the string STR to the file.

:/Str1/,/str2/W file forward search, and write the line containing the string str1 to the line containing the string str2 into the file File

 

 

 

 

1. Simple replacement expression

The replacement command can replace another word with one word in the full text:

: % S/four/4/g

The prefix of "%" indicates that replacement is performed in all rows. The last "G" Mark replaces all matching points in the row. If you only operate on the current row, remove %.

If you have a word like "thirtyfour", the above command will fail. In this case, the word is replaced with "thirty4 ″. To solve this problem, use "\ <" to specify the start of the matching word:

: % S/\ <four/4/g

Obviously, errors will still occur when processing "fourty. Use "\>" to solve this problem:

: % S/\ <four \>/4/g

If you are coding, you may just want to replace the "four" in the comment, but keep the "four" in the code. Since this is difficult to specify, you can add a "C" mark in the replacement command, so that Vim will prompt you before each replacement:

: % S/\ <four \>/4/GC

2. Delete unnecessary spaces.

To delete unnecessary spaces behind each line, run the following command:

: % S/\ s \ + $ //

The command specifies that the range is "%", so this will apply to the entire file ." The matching mode of the substitute command is

\ S \ + $ ". This indicates one or more (\ +) spaces (\ s) before the end of the line ($ ). The "to" part of the replacement command is empty :"//". In this way, the matching spaces are deleted.

3. Matching repetition Mode

The asterisk (*) specifies that the items before it can be repeated at any time. Therefore:

/*

Match "A", "AA", "AAA", and so on. But it also matches "" (Null String), because zero times are also included. The asterisk "*" is only applied to the item next to it. Therefore, "AB *" matches "A", "AB", "ABB", and "abbb. To repeat the entire string multiple times, the string must be an item. Add "\ (" and "\)" before the component. Therefore, this command:

/\ (AB \)*

Matching: "AB", "Abab", "ababab", and so on. It also matches "".

To avoid matching null strings, use \ + ". This indicates that the previous item can be matched once or multiple times.

/AB \ +

Match "AB", "ABB", "abbb", and so on. It does not match "A" that follows "B ".

To match an option, use "\ = ". For example:

/Folders \ =

Matches "folder" and "Folders ".

4. specify the number of repetitions

To match a specific number of times of repetition, use the form "\ {n, m. "N" and "m" are numbers. The item before it will be repeated "N" to "M" Times (| aggressive sive | contains "N" and "m "). For example:

/AB \ {3, 5}

Match "abbb", "abbbb", and "abbbbb ".

When "N" is omitted, It is 0 by default. When M is omitted, It is infinitely large by default. When ", M" is omitted, it indicates that the repetition is exactly "N" times. For example:

Number of pattern matches

\ {, 4} 0, 1, 2, 3, or 4

\ {3,} 3, 4, 5, and so on

\ {0, 1} 0 or 1, same as \ =

\ {0,} 0 or more, the same *

\ {1,} 1 or more, same as \ +

\ {3} 3

5. Select one or more matches.

In a search mode, the "or" operator is "\ | ". For example:

/Foo \ | bar

This command matches "foo" or "bar ". More options can be connected to the following:

/One \ | two \ | three

Match "one", "two", or "three ".

To match multiple times, the entire decision structure must be placed between "\ (" and:

/\ (FOO \ | bar \) \ +

This command matches "foo", "foobar", "foofoo", "barfoobar", and so on.

Another example:

/End \ (if \ | while \ | \)

This command matches "endif", "endwhile", and "endfor ".

 

In VI/vim, you can use the: s command to replace the string. In the past, we used only one format to replace the full text. Today we found that there are many ways to write this command (VI is really powerful, and there are still a lot to learn). Record several methods here to facilitate future queries.

 

: S/Vivian/sky/Replace the first Vivian in the current row with sky

: S/Vivian/sky/g replace all Vivian in the current row with sky

 

: N, $ S/Vivian/sky/Replace the first Vivian from row n to row N as sky

: N, $ S/Vivian/sky/g replace all Vivian values from row n to row n

N is a number. If n is., it indicates starting from the current row to the last row.

 

: % S/Vivian/sky/(equivalent to: G/Vivian/S // sky/) Replace the first Vivian of each row with sky

: % S/Vivian/sky/g (equivalent to: G/Vivian/S // sky/g) replace all

 

You can use # As the separator. The/in the middle will not be used as the separator.

: S # Vivian/# Sky/# Replace the first Vivian/in the current line with Sky/

 

: % S +/oradata/apras/+/user01/apras1 + (replace with +/):/oradata/apras/Replace with/user01/apras1/

 

1.: S/Vivian/sky/Replace the first Vivian in the current row with sky

: S/Vivian/sky/g replace all Vivian in the current row with sky

 

2.: N, $ S/Vivian/sky/Replace the first Vivian from row n to row N with sky

: N, $ S/Vivian/sky/g replace all Vivian values from row n to row n

(N is a number. If n is., it indicates starting from the current row to the last row)

 

3. Replace % S/Vivian/sky/(equivalent to: G/Vivian/S // sky/) with the first Vivian of each row as sky

: % S/Vivian/sky/g (equivalent to: G/Vivian/S // sky/g) replace all

 

4. You can use # As the separator. The/in the middle will not be used as the separator.

: S # Vivian/# Sky/# Replace the first Vivian/in the current line with Sky/

 

5. Delete ^ m from the text

Problem description: For line breaks, use the carriage return line break (0a0d) in the window, and the carriage return (0a) in Linux. In this way, when you copy files on Windows to Unix, there will always be a ^ m. Please write a shell or C program that is used in UNIX to filter Windows files (0d.

· Use the command: CAT filename1 | tr-D "^ V ^ m"> newfile;

· Run the following command: sed-e "s/^ V ^ m/" FILENAME> outputfilename. Note that in methods 1 and 2, ^ V and ^ m indicate Ctrl + V and CTRL + M. You must enter the file manually instead of pasting it.

· Processing in VI: First open the file using vi, Press ESC, and then enter the command: % S/^ V ^ m //.

·: % S/^ m $/g

If the above method is useless, the correct solution is:

· Tr-d "\ r" <SRC> dest

· Tr-d "\ 015" dest

· Strings A> B

 

6. Others

Use the S command to replace strings. The specific usage includes:

: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line

: S/str1/str2/g replace all the str1 strings in the row with str2

:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2

: 1, $ S/str1/str2/g replace str1 with string str2

: G/str1/S // str2/g functions are the same as above

From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only search

String is replaced for the first time. G is placed at the beginning of the command to replace all rows containing the search string in the body.

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.