https://stackoverflow.com/questions/12867573/searching-from-end-of-file-using-vim
43
How do I search from the end of file using VIM? I want to search for the last occurrence of XXXX in the open file.
Add a comment
5 Answers
57
Type G$?XXXX
G
to go to the end of the file$
to go to the end of the line?
to search backwardsXXXX
being what you want to search for
You could also do gg?XXXX
which goes to the start of the file, then searches backwards, wrapping around to the end of the file. However for large files it can be slow to seek to the start of the file and then immediately to the end of the file.
40
My suggestion is to use a range combined with searching backwards via ?
.
:1?XXXX
Overview:
1?XXXX
is the range.- The
1
means first line of the file. ?XXXX
means search backwards from the first line (wrapping around) until you find the patternXXXX
As ZyX mentioned this relies on wrapscan
to be set, which it is by default. See :h 'wrapscan'
for more information.