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)?


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.

| improve this answer | |
  • 1
    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 Aug 13 '15 at 15:26
  • 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 Nov 6 '17 at 13:59
  • There is mush simpler gD command. suggested by @Ben below; – aryndin Sep 26 '19 at 8:52

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].

| improve this answer | |
  • 2
    +1 that is obscure! You can also use :ij 2 foo to go to the second one, etc. – evilsoup Nov 19 '13 at 18:46
  • Thanks, didn't know that command! Any idea how to get to the last occurrence? – Philipp Moers Jun 5 '17 at 10:47
  • @PhilippMoers, do :ilist and choose the last occurrence. – romainl Jun 5 '17 at 17:40
  • 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 Jun 5 '17 at 22:22
  • :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 Jun 6 '17 at 5:48

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


| improve this answer | |

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.

| improve this answer | |
  • This only works for words recognized as a "variable", not for arbitrary words. Ref: help gD – YvesgereY Oct 8 '19 at 10:34

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


(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

| improve this answer | |

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.

| improve this answer | |

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".

| improve this answer | |
  • 1
    Welcome 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 May 2 '17 at 12:54

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.