17

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

8 Answers 8

33

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.

3
  • 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, 2015 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, 2017 at 13:59
  • There is mush simpler gD command. suggested by @Ben below;
    – aryndin
    Sep 26, 2019 at 8:52
12

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

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

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

gg

/foo
0
4

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.

1
  • This only works for words recognized as a "variable", not for arbitrary words. Ref: help gD
    – YvesgereY
    Oct 8, 2019 at 10:34
3

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

1
  • Oof. That's quite a key combination! But thanks - I've been looking for this!
    – user24925
    May 13, 2022 at 0:39
2

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.

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

1
  • 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. May 2, 2017 at 12:54
0

Real late here but in case this helps someone, I think the "POSIX" way to "jump" to the first match with a "proper" vi or ex (no vim-like extensions required) is closer to:

:1;/foo/;

Start on line 1

From there, find to the first line containing pattern foo

Then repeat the pattern search (on that same line) by using ;.

ex is expected to work on characters (not lines) and open at the end of a file by default.

Likewise, something like this is more POSIX Recall that vi is supposed to work on lines and open on the first row by default.

$ vi "+/foo/;" file.txt

These should jump you to the first character of the first occurrence of foo, no matter where you are in the buffer / file even without "vim powers."

You might need something more along the lines of ~

:set searchincr
:1;/foo/;/

Another method ~

:1^M/foo

^M is a literal input using <control>+v then <enter/return> You might need to play with it to meet the requirements of your implementation.

YMMV

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .