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.
5 Answers
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.
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.
1)Use Vi editor
vi xx.log
2)Use Shift + g
3)for searching pattern :?xxx
replace this xxx with your search string.
4)Shift + n
for next search
One way is:
gg?<string>
That will put the cursor at the the beginning of the file and perform a backward search. Since it will be impossible for vim to go before the cursor position, it will perform a search starting from the very end of the file.