10

After I search for "foo" by doing "?foo", I can search forward by "n" or backward by "N", however sometimes the n/N directions are flipped in the middle of search. If I restart Vim, it's OK again. What key sequence may I have pressed to cause this? How to revert back to normal mode?

I don't recall that I ever ran into this in old Vi.

EDIT: it's not the Caps Lock key. Thanks for the suggestion though.

  • I guess it's not just the wrapping around at the end/beginning of the file? Or maybe you typed * or something that reverses the search? I know I unintentionally do it a lot of time. – Dave Vogt Aug 9 '10 at 16:07
  • @Dave Vogt, it's not just end/beginning of the file. I just tested "", you are right, if I press "" I can change/correct the search direction. If you put in an answer, I will vote it up and accept it. Thanks. – grokus Aug 9 '10 at 16:15
11

This can happen if you started out searching backwards with ?foobar, and you pressed / in the middle. Now you're searching forward, so n and N are switched relative to what you used to have. The same thing will happen if you started with / and pressed ? in the middle.

  • Sir you are right. I just tested what you said. Could you tell me what role "*" is playing in this midst? – grokus Aug 9 '10 at 17:47
  • 1
    @grokus: * means to search forward for whatever word is under the cursor. – Daenyth Aug 9 '10 at 18:05
  • @Daenyth, thanks. I always thought that I wasn't too bad at Vim. (I could get things done) Today I'm reconsidering my position. – grokus Aug 9 '10 at 18:36
  • and to follow up @Daenyth, you can search backward for whatever word is under the cursor using #. These will match only on the full word. To search for the word under the cursor, including inside other words, use g* – Nathan Fellman Aug 9 '10 at 19:06
3

It sounds stupid but did you checked Caps-Lock?

  • yes I did. It was on "off" position. I have seen this behavior so many times so I was finally fed up and decided to get to the bottom of this. – grokus Aug 9 '10 at 16:03
0

Nathan has already answered why this happens, but if you use both directions regularly and want n to always look ahead and N to always look back, no matter which way you searched, you can put this in your .vimrc:

nmap n /<CR>
nmap N ?<CR>

/ and ? without parameters perform the last search, in their respective (absolute) directions.

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.