31

I want to search a word from my current cursor position in vim to upward in file.

How to achieve this?

Also how to do same for downward in file.

1
  • The :set hlsearch and :set incsearch commands might be of interest to you. Commented Sep 3, 2013 at 15:22

2 Answers 2

44

To search in reverse from your cursor for a word, just use ?. So to find the word "fred" you would issue ?fred.

For forward searching you use /, using "fred" as an example again you would issue /fred.

If you want to continue searching for the same term, in the same direction you can use the n command. (Or you can issue ? or / without arguments).

2
  • Since OP searches for a word from its current cursor position which I understand as word below cursor, he can also use the star * operator to search downward. There is also an equivalent for going upward but I do not rember it and it is exactly what I am looking for. Commented Jun 29, 2023 at 10:24
  • * (forward) and # (backwards) search for the word under the cursor. No need to type the word with ? Commented Jun 29, 2023 at 10:34
19

Typing ?search text in command mode searches backward from point while /search text searches forward.

Typing n goes to the next instance of the search text in the direction you were last searching and typing N looks in the opposite direction of your last search.

1
  • Shift+N so intuitive. Why bother with ? at all? Commented May 27, 2022 at 16:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.