How can I switch from forward to backward search in Vim?

35

9

In Vim, I know that / will search forward from the cursor, with n and N repeating the search forward and backward, respectively.

I also know that ? will search backward from the cursor, with n and N repeating the search backward and forward, respectively.

My question is, if I've done a ? search, and I want to re-run that as a / search (so I don't have to keep holding shift to move forward), is there a way to switch the previous search's direction without retyping the search term?

Nathan Long

Posted 2011-02-01T17:28:20.023

Reputation: 20 371

Answers

49

Simply typing a ? without any text following it will re-run the search backwards.

I.e. typing / Return or ? Return will always rerun the last search, but in the direction you want.

Wes Hardaker

Posted 2011-02-01T17:28:20.023

Reputation: 1 796

Just a minor thing (looks pretty obvious), but I did miss it. You have to navigate to end of the file (ctrl + end) to reverse search it. – dimuthu – 2017-10-24T05:27:30.063

Wow, that was way easier than I thought. :) Thanks! – Nathan Long – 2011-02-08T14:36:12.183

Hmmm, and apparently, ? or / followed by the up key cycles through a common history for the two. I thought each had their own history. – Nathan Long – 2011-02-08T14:40:08.760

1

Is this more easier?

ctrl-o, ctrl-i - go through jump locations

Searching REF

Joshua

Posted 2011-02-01T17:28:20.023

Reputation: 131