Linux VIM Editor Find and Replace Tutorial

  

VI/VIM is one of the most commonly used tools for programmers, especially on unix/linux; and lookup replacement is one of the most frequent operations.


Generally in vim we use this method to replace: %s/dest/new/g, where dest is the search target, new is the new content to replace, g is the full text replacement The general vim configuration is the default full text replacement, so g can also be omitted.


The following summarizes some of the commonly used replacement modes for reference learning; the content is transferred from the network, the original source has no source information!



Summary of the replacement patterns commonly used in VIM.


0,:g/null/d

Find the null line and delete


1, simply replace the expression

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

:%s/four/4/g

“%” Range prefix representation Perform a replacement in all rows. The last “g” tag indicates that all matching points in the row are replaced. If you only want to operate on the current line, just remove %.

If you have a word like “thirtyfour”, the above command will go wrong. In this case, the word will be replaced with ”thirty4″. To solve this problem, use “\\<” to specify the beginning of the matching word:

:%s/\\<four/4/g

Obviously, this is in the process“ Fourty” will still go wrong. Use “\\>” to solve this problem:

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

If you are coding, you may only want to replace In the comments, “four”, and retain the code. Since this is hard to specify, you can add a &cquo;c” tag to the replacement command so that Vim will prompt you before each replacement:

:%s/\\<four\\>/4 /gc



2, remove extra spaces

To delete extra spaces after each line, you can execute the following command:

:%s/\\s\\+$//

The range specified in the command is “%”, so this will apply to the entire file. The matching pattern of the ”substitute” command is

“\\s\\+$”. This represents one or more (\\+) spaces (\\s) before the end of the line ($). The “to” section of the replacement command is empty: ”//”. This will remove those matching whitespace characters.



3, Matching Repetitive Patterns

Asterisk Items “*” Specifies that items preceding it can be repeated any number of times. So:

/a*

match “a”,”aa”,”aaa”, and so on. But it also matches “& quoquo; (empty string), because zero is also included. The asterisk “*” applies only to the item immediately preceding it. So “ab*” matches “a”,”ab”,”abb”,”abbb”, and so on. If you want to repeat the entire string multiple times, the string must be composed of an item. The way to form an item is to add “\\(”, followed by “\\)” in front of it. So this command:

/\\(ab\\)*

match: “ab”,”abab”,”ababab”, and so on. And it also matches “”.

To avoid matching empty strings, use “\\+”. This means that the previous item can be matched one or more times.

/ab\\+

Match “ab”,”abb”,”abbb”, and so on. It doesn't match “a” that doesn't follow “b”.

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

/folders\\=

match “folder” and “folders”.



4, specify the number of repetitions

To match a specific number of repetitions, use “\\{n,m}” This form. Where “n” and “m” are numbers. The item in front of it will be repeated “n” to “m” times (| Inclusive|  Contains “n” and “m”). For example:

/ab\\{3,5}

match “abbb”,”abbbb” and “abbbbb”.

When “n” is omitted, it is defaulted to zero. When “m” is omitted, it is defaulted to infinity. When “,m” is omitted, it means that it repeats exactly “n” times. For example:

Number of pattern matches

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

\\{3,} 3,4,5, etc.

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

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

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

\\{3} 3



5, choose one match

in one lookup In the pattern, the ” or ” operator is “\\| ”. For example:

/foo\\| Bar

This command matches “foo” or “bar”. More choices can be connected later:

/one\\| Two\\| Three

Match “one”,”two” or “three”.

To match multiple iterations, the entire selection structure must be placed between “\\(” and “\\)”:

/\\(foo\\| Bar\\)\\+

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

Another example:

/end\\(if\\| While\\| For\\)

This command matches “endif”,”endwhile” and “endfor”.

Copyright © Windows knowledge All Rights Reserved