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".
I find
G$N
slightly awkward to type. Instead just doggN
and obviously if you already didggn
to be on the first result and want to hit the last result doN
. – mcanfield – 2015-08-13T15:26:37.747note 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