Kvass
Kvass

Reputation: 8434

Vim - confusing search behavior for repeated character

Say I have a line with a repeating character and my cursor is at the start of the line, e.g.

[=]===================

If I run /=, the = sign just after the cursor is highlighted, as expected.

=[=]==================

However, if I then search for /==, I would expect

=[==]=================

but instead I get

==[==]================

Likewise, for /=== I would expect

=[===]================

but instead I get

===[===]==============

Can someone explain this behavior? Is this a bug or by design?

EDIT

I see even stranger behavior when searching backwards. Say my cursor is just below that line:

?=: (expected)

===================[=]

?==: (expected)

==================[==]

?===: (expected)

=================[===]

?====: (unexpected)

===============[====]==

?=====: (unexpected)

============[=====]===

?======: (expected)

==============[======]

?=======: (unexpected)

============[=======]=

?========: (unexpected)

======[========]======

I'm really curious to hear an explanation for this behavior.

Upvotes: 2

Views: 129

Answers (1)

Walter A
Walter A

Reputation: 19982

It is strange indeed. Vim seems to look for matches from the beginning of the line and then compares the match with your cursor position.

12[3]212321

Now you want to go to the 5th position and you look for 12321.
The first match on the line is from position 1. That will match

[12321]2321

Since the 1on the fifth position is already used for the first match, vim will not see the match

1232[12321]

In your case, with /=== from

[=]===================

the first match on the line is skipped, and the cursor will jump to the second match.

===[===]==============

Edit: Searching backwards will also count the matches from the beginning of the line. When you have a 20 character string

====================

and looking back for ?=== will start making matches from the beginning of the line:

=== === === === === === ==

and will match

=== === === === ===[===]==

Matching will still be done reading from left to right, so you will not find an apple here:

reversed elppa

Upvotes: 2

Related Questions