Vim search using regex

I want to find a line starting with "abc" and ending with "xyz" in vim.

The following are the commands I tried:

:1,$g/abc[\w\W]*xyz/ :1,$g/abc\[\\w\\W\]\*xyz/ :1,$g/abc*xyz/ 

"[\ w \ W] *" means that the texts between "abc" and "xyz" can be any characters

"1, $" means that the search range is from the first line to the last line in the file opened by vim.

I found that the search pattern

 abc[\w\W]*xyz 

works at https://regex101.com/

why doesn't it work in vim?

+20
source share
4 answers

The command below should work if “any character” means something different to you than to Vim:

 :g/abc.*xyz 
  • . means "any character except EOL".
  • * means "any number (including 0) of the previous atom".
  • 1,$ can be reduced to % .
  • :global works all over the default buffer, so you don’t even need % .
  • Closing / not required if you do not follow :g/pattern with the command, as in :g/foo/d .
+20
source

As soon as the file gets too large (say 1 GB), ": g / abc. * Xyz" gets pretty slow.

I found that

 cat fileName | grep abc | grep xyz >> searchResult.txt 

more efficient than using vim's search function.

I know that this method can return strings starting with "xyz" and ending with "abc".

But since this is a rare case in my file (and maybe it doesn’t happen so often for other people), I think I should write this method here.

+3
source

It seems that character classes such as \ w cannot be used within the syntax of the collection [..] , perhaps because it checks the strategy character by character. From :h /[] :

Matching a collection can be slow, as each character in the text needs to be compared with every character in the collection. If possible, use one of the other atoms above. Example: "\ d" is much faster than "[0-9]" and matches the same characters.

However, you can use similar functions specially prepared for the syntax [..] . C :h /[] again:

The expression of a character class is evaluated by the set of characters belonging to this character class.

Examples

include:

 [:alnum:] letters and digits [:alpha:] letters [:blank:] space and tab characters [:cntrl:] control characters [:digit:] decimal digits [:graph:] printable characters excluding space [:lower:] lowercase letters 
0
source

If you want to find them one by one, you can click

/

and then write

abc.*xyz

and press Enter to find the first occurrence of the template. Then use n for the next occurrence and Shift + n for the previous one. I usually do this because it is easier for me to change.

0
source

Source: https://habr.com/ru/post/989505/


All Articles