4

I'm using Vim 7.4. I tried to search backward for front and frnt, so I pressed ? and entered the regular expression fro\?nt. Vim tells me that it can't find fro?nt (the backslash is missing in the message). None of the occurances that I can see are highlighted.

On the other hand, if I search forward, it finds them just fine. Even if I am at the end of the file and can't find anymore by searching forward, Vim recognizes their occurance earlier in the file by highlighting them. In that case, the message is that it can't find fro\?nt, i.e., the backslash is not missing in the message.

I get that pressing ? to search backward might cause a problem with the \? in the regular expression, but I would have thought that the backslash would have removed any doubt that question mark in the regular expression is not a delimiter. I was wondering if this was the actual intended behaviour. I couldn't find anything in the help that speaks to this. I also see the same behaviour for gVim complied for Windows 7 as well as the version packaged with Cygwin.

  • 1
    I remember noticing something similar a long time ago and I'd simply do a forward search followed by "N". I tested your case and I do agree that it should probably be changed. – Julie Pelletier Jun 12 '16 at 5:38
  • Yes, that's what I ended up doing. Thanks. – user36800 Jun 13 '16 at 15:00
7

This is because of search feature called offsets. From :help 27.3

*27.3*  Offsets

By default, the search command leaves the cursor positioned on the beginning
of the pattern.  You can tell Vim to leave it some other place by specifying
an offset.  For the forward search command "/", the offset is specified by
appending a slash (/) and the offset: >

    /default/2

This command searches for the pattern "default" and then moves to the
beginning of the second line past the pattern.  Using this command on the
paragraph above, Vim finds the word "default" in the first line.  Then the
cursor is moved two lines down and lands on "an offset".

...

SEARCHING BACKWARDS

The "?" command uses offsets in the same way, but you must use "?" to separate
the offset from the pattern, instead of "/": >

This is definitely the intended behavior. From :help \?

                            */\?*
\?  Just like \=.  Cannot be used when searching backwards with the "?"
    command. {not in Vi}

The solution is pretty simple. Just use \= instead of \?. That works for me.

  • Thanks, DJ. Pity that the backslash doesn't signal that the following question mark is not a delimiter. Afterall, a backslash by itself doesn't mean anything, at least in the default magicness. But it is documented, and I can see that it might cause problems to start changing that now. Also, there are probably situations that benefit from the current behaviour, even that I may not be aware of them. – user36800 Jun 13 '16 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.