7

When I do a /someStringtoFind in vim it immediately jumps to the closest matched string.

Almost always this is what I want it to do. Sometimes though, I want to be able to enter a string to find and not have my cursor move while I am typing the string. Then, after I have finished typing the string, I could hit enter for vim to jump to the string.

How can I do this?

3
  • 6
    incsearch is either on or off. – romainl Dec 31 '15 at 19:24
  • 3
    You could also try g/. – Karl Yngve Lervåg Dec 31 '15 at 19:50
  • Then i guess I could write a map to turn incsearch on and off would what work for me. – red888 Dec 31 '15 at 20:07
9

Type : before searching: :/someStringtoFind Enter. If the match isn't at the beginning of the line, press n to go to the beginning of the match.

/someStringtoFind/ is an empty ex command with the range of lines /someStringtoFind/ (a 1-line range). The empty ex command means “jump to the end of the range”, so the command jumps to the next line containing a match for someStringtoFind. You can drop the final / if you have nothing to put afterwards.

:/someStringtoFind drops you to the beginning of the target line. The search is stored in the search history, so n brings you to the beginning of the next match, which is on the current line. There's an exception: if the match starts at the beginning of the line, then you're already at the beginning of the match, and n would bring you to the next match. As long as the match doesn't span lines, $N brings you to the beginning of the match reliably, if you don't mind the extra typing (or are writing a macro).

4

You can make use of the fact, that you can set the search register @/ directly, which does not move the cursor. So simply do :let @/='foobar' and the cursor position won't change. If you want to move to the match, you press n as usual.

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.