How do I go to the first search result in a file?

10

1

Is there a command in vim for navigating to the first search result in a file (i.e. the one with the lowest line number)? Say, if I do /foo, can I find the first instance of foo in the file (not starting from where I am in the file, but starting from the beginning of the file)?

Jonathan

Posted 2013-11-19T15:19:15.087

Reputation: 1 539

Answers

17

ggn will bring you to the first instance of your search in the file -- the gg brings you the beginning of the file and the n goes to the next match of your pattern.

You can also use gg2n to go to the second instance in the file, or gg42n to go to the forty-second instance. You can similarly use G$N to go to the final match (G brings you to the final line, $ brings you to the end of that line, and N searches backwards), or G$2N to go to the penultimate one.

evilsoup

Posted 2013-11-19T15:19:15.087

Reputation: 10 085

I find G$N slightly awkward to type. Instead just do ggN and obviously if you already did ggn to be on the first result and want to hit the last result do N. – mcanfield – 2015-08-13T15:26:37.747

note if you are searching backwards this will give you the last result in the file... which makes sense but just confused me, forgot I'd pressed #!! – JonnyRaa – 2017-11-06T13:59:07.850

There is mush simpler gD command. suggested by @Ben below; – aryndin – 2019-09-26T08:52:00.810

8

Obscure Ex commands for the win:

:ij[ump] foo  "jumps to first foo that is not in a comment
:ij[ump]! foo "jumps to first foo, comment or not

See also :il[ist].

romainl

Posted 2013-11-19T15:19:15.087

Reputation: 19 227

Thanks, didn't know that command! Any idea how to get to the last occurrence? – Philipp Moers – 2017-06-05T10:47:01.563

@PhilippMoers, do :ilist and choose the last occurrence. – romainl – 2017-06-05T17:40:50.290

Thanks, but that's not quite what I want. I want a non-interactive keybinding that immediately jumps to the last occurrence of the latest search term (like G$N does, but without polluting the jumplist). For the first occurrence, I have nmap gö :ijump! 1 /<C-R>/<CR>, but I can't figure out the same thing for the last occurrence. – Philipp Moers – 2017-06-05T22:22:19.600

:ijump uses a number which will always be different so I'm afraid there's no "easy" way to get what you want. You should try with a function that grabs the output of :ilist and parses it to get the number of the last occurrence. – romainl – 2017-06-06T05:48:35.557

2+1 that is obscure! You can also use :ij 2 foo to go to the second one, etc. – evilsoup – 2013-11-19T18:46:34.503

5

Use the gg command to navigate to the beginning of the file.

gg

/foo

Moyamo

Posted 2013-11-19T15:19:15.087

Reputation: 221

3

The gD command will search from the top of the file for the word under the cursor.

The related gd command will search from the top of the current c-like function.

Ben

Posted 2013-11-19T15:19:15.087

Reputation: 2 050

This only works for words recognized as a "variable", not for arbitrary words. Ref: help gD – YvesgereY – 2019-10-08T10:34:45.040

2

Another cool shortcut if your cursor is on one of the matches in the file:

[<C-i>

(Just to be clear that's the left bracket ([) followed by control-i)

This is like using the "*" command but having it go to the first match in the file rather than the next match after the cursor

Jonathan.Brink

Posted 2013-11-19T15:19:15.087

Reputation: 141

1

Methods relying on using gg and then searching through either /foo or n are fine for interactive use, but when batch editing with Vim's -S argument they won't work if the searched word is the first word in the file; in that case your cursor will be positioned at the second occurence.

The foolproof method is an ex command :0/foo. Issuing only an address in ex command will take you to that address, so :12 will take you to the twelfth line. ed/ex/Vim support search patterns as addresses, so :/foo will take you to the first match from the cursor position (implicit "." at the beginning). :0/foo means "first 'foo' from the zero'th line".

urxvtcd

Posted 2013-11-19T15:19:15.087

Reputation: 11

1Welcome to Super User. Your answer could be made more complete by including an example and some additional explanation about the proposed command. Thanks for contributing. – I say Reinstate Monica – 2017-05-02T12:54:02.903

1

This is rather too late, but the answer may help someone. This is not a command to be executed inside vim, rather passing the search word as an argument while opening the file from linux command line. type vi filename +/foo on linux command line. vi will open the file directly to line where the first occurrence of the word 'foo' is.

madD7

Posted 2013-11-19T15:19:15.087

Reputation: 111