当前位置:网站首页>VIM from dislike to dependence (18) -- advanced search mode

VIM from dislike to dependence (18) -- advanced search mode

2022-06-22 03:28:00 aluluka

In the last article , We got to know how to use the search pattern , Can also pass through n and N Search for . This article will introduce more advanced usage in search . In addition, when I wrote an article, I found that I couldn't use the dynamic diagram to demonstrate when introducing and searching the relevant content , The main reason is that there are too many entries , It's too big to cut into a moving picture , May not be able to upload . The second is to enable the match highlight option , The matching result can be seen more intuitively than the starting diagram . So this article will not use the dynamic graph .

Case sensitive

By default , It is case sensitive when searching , For example, in the following example , We can't match the upper case REQUIRE Of

 Insert picture description here
have access to ignorecase Item to remove case sensitivity , For example, in neovim Write the following configuration in

vim.o.ignorecase = true

Or in vim In the configuration file of

set ignorecase

At this time, enter require when , It is found that... Can be matched REQUIRE
 Insert picture description here
But in practice, we hope that sometimes case sensitivity , Sometimes case insensitive , For example, when you want to search a function or variable vaguely . A better approach is to set up smartcase term . It enables case sensitivity only if the input character has uppercase , Otherwise, it is case insensitive .

vim.o.smartcase = true
set smartcase

Of course, we can also specify whether this search is case sensitive at each search . You can enter... When matching \c To use case insensitive \C Case sensitive , This symbol can appear anywhere , Even if you enter /requ\Cire It can also correctly find all require character string .
 Insert picture description here

Use regular expressions to match

vim Support regular expression search ,vim It's using POXIS Rules for regular expressions of , Let's get used to it Perl Regular expression rules are not suitable for people who use them . Now let's take a concrete example

Suppose there's a css file , I need to search inside 16 Binary color

body {
     color: #3c3c3c; } 
a {
     color: #0000EE; } 
strong {
     color: #000; }

We enter such a regular expression when searching #([0-9a-fA-F]{6}|[0-9a-fA-F]{3}) . If it is found, an error will be reported , But for regular expressions , That's fine , What we want to match is to # start , In the back 6 A or 3 individual 16 The character of a hexadecimal number .

This is because there are special characters in it , Need to escape , for example () stay vim There are special uses in , We will introduce its use in the following . We need to write the regular expression like this #\([0-9a-fA-F]\{6\}\|[0-9a-fA-F]\{3\}\)
 Insert picture description here
good heavens , The backslash has 7 individual , and (){} Need to escape , and [] There is no need to escape . Regular expressions are troublesome enough , And remember vim Different from other editors , People will feel numb after one use .

Fortunately vim Provides very magic Pattern , In addition to _ 、 Numbers 、 All characters except letters have special meanings , So we don't have to worry about what needs to be escaped , What doesn't need . You can add... At the beginning of the search \v To enable this mode , That is, we can enter \v#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})
 Insert picture description here
We can also use \x Express 16 Hexadecimal number , To simplify the above regular expression , \v#(\x{6}|\x{3}).
 Insert picture description here
\x yes vim A member of a character class , I think the following are more useful

character meaning
\x Hexadecimal number
\X Non hexadecimal numbers
\d Numbers
\D The digital
\o Octal number
\o Non octal numbers
\w Include letters 、 Numbers and _
\W barring Letter 、 Numbers and _
\h Include Letters and _
\H Letters and... Are not included _
\l Lowercase letters
\L Non lowercase letters
\u Capital
\U Non capital letters

Except for very magic Pattern ,vim There is also very nomagic Pattern , In this mode, all characters only represent themselves , No special meaning , for example . Representing a single character in a regular representation , But in very nomagic In the pattern, it represents a point , It can match the character itself , To enable the very nomagic Pattern , have access to \V As a prefix .

Use parentheses to get child matches

stay vim Can be used in <(\w+)\_s+\1> To match repeated words , for example

I love python
python is so good

In this sentence we can match python The word .
 Insert picture description here
Let's look at this regular expression ,<> Match words that begin or end with certain characters , for example <Py Will match all with Py The first word , and on> Will match all with on Ending words , Because our requirements here do not require matching words that begin with certain characters , Adding this limits us to matching words instead of certain characters .\_ You can add vim Any character in a character class , Means to match the newline character on the original basis , for example \x Can match 16 Hexadecimal number ,\_x Can match 16 Hexadecimal numbers and line breaks .\_s+ It's a match <Tab> White space characters and line breaks , And it's a match 1 One or more .

stay vim Brackets are used in to represent child matches , It is a child of the entire regular expression match , for example Py(tho)n It can match Python and Python In the string tho.\ The number added to the back represents the number of matches , The first 0 Matches are matches of the entire regular expression ,1、2、3、…、n Then it corresponds to the 1 Child matches , the second 、 The first n Child matches .

I know that , We can read the whole regular expression , It matches such a word : He is an arbitrary word , But it needs to be followed by a word like him , Whether there are multiple spaces in the middle 、 Tab or newline .

If we just want to match whether there are multiple duplicates Python It can be written like this : (<Py\w+on>)\_s+\1

Define the matching range

In search mode ,vim Search the contents entered in the field ( It can be a regular expression or a string matching the original meaning ) And the highlighted text that it matches . Generally, the contents of the lookup domain are called patterns , The highlighted text is called a match . A pattern can correspond to multiple matches ( The meaning of the mode here is different from that of the normal mode and the insert mode mentioned above ).

A matching boundary usually corresponds to the beginning and end of a pattern . for example <> Indicates that a matching boundary will be a word . In addition to this ,vim Provides \zs and \ze These two metacharacters are used to crop a match .\zs Indicates that the specified part at the beginning of the match is removed 、\ze Indicates that the specified part at the end of the match is removed .

For example, in the above sentence , I passed <\w+> Match to all words . And then through <\zsPy\w+> To crop the matching content , All words will be highlighted Py And what follows , If not by Py The beginning is completely cut out . Or use <\w+\zeon> To cut , Only show all words on Front part .

Escapes special characters

Here we use a URL As an example

https://www.baidu.com/search?q=\\/

Suppose we want to match this... That appears in all documents url, How to do it ?

First consider entering a large string of contents into the matching pattern , But this big string doesn't need to be entered manually . We can vim The command pattern and the current matching pattern are treated as text in a special insertion pattern , So we can use the example of using registers in input mode described earlier . First use "iy$ Paste a line , Then use... In the matching pattern <Ctrl+r>i To paste .
 Insert picture description here
But at this time, we find that the matching result is not what we want , This is because in the matching pattern / Is a special character with a special meaning , We need to tell vim Interpret it as ordinary characters , It can be used at this time \V To enter very nomagic Pattern , The pattern is related to very magic contrary , Interpret all characters as normal characters .

We'll show that it only matches https:, And the string in the schema becomes https: , From behind / Start truncating , At this time, we can use \/ Yes // convert . meanwhile \ As a special character , We also need to escape it . That is, the entire match should be entered https:\/\/www.baidu.com\/search?q=\\\\\/
 Insert picture description here
This chapter mainly describes how to vim Using regular expressions , This should be the end of the conversation vim Find the basic operations in the mode . Later, we will introduce how to perform the replacement operation .

原网站

版权声明
本文为[aluluka]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220314218670.html