Vim Regex Column Position Match

I learn about the Vim pattern and am very confused about determining the correspondence of a column. Below I do a very simple test. Just create the file in the first row of the first column: 123456789 . Just let me easily track the column number where each digit is located. Then I search /.\%>3c.*\%<8c , it matches 3456 and seems reasonable, because since the document explains that \%<8c will match the 7th column, and it will match the zero width, so it will only match 6. But then I look for /\%>3c.*\%<8c , this time Vim matches 4567 . So why this time it corresponds to 7 ??? This seems unfounded. My version of Vim has been updated: 7.4. Included patches: 1-884.

+5
source share
2 answers

If your goal is to look for column positions greater than 3 and less than 8, you do not need points or stars, this is enough:

 /\%>3\%<8 

If you are wondering about the unintuitive behavior of adding dots and / or stars to your search, then yes, this is confusing. I believe that in this case the star is superfluous. You can get the same behavior that you see (numbers 4-6 are found) simply by doing a search:

/ \%> 3 \% <8.

I believe that a point is considered a restrictive search criterion. In other words, there must be a char extending the position of the column. Thus, the search procedure works something like this: is there a column position 4 with a character in the 5th position? yes, add it to the result; is there a column position 6 with char at position 7? yes add it; is there a column position of 7 with char in eighth position? No - because criteria of zero width do not include 8th position or higher (\% <8). To turn char into an eight-position position, you can add another point after 8c and then find 4-7, for example:

 /\%3c.\%<8c. 

But, mind you, this just brings us back to my first example:

  /\%>3\%<8 <=> /\%3c.\%<8c. 
+1
source

I think I have an answer. There are actually two matches, the first match is 456, the second match is an empty line in column 7. When installing hlsearch, 4567 will be highlighted, since the second match is an empty line in column 7, to make the match visible, vim will also highlight the character in this column. This is what I think, please correct me if I am wrong.

0
source

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


All Articles