Searching from end of file using VIM


https://stackoverflow.com/questions/12867573/searching-from-end-of-file-using-vim

Searching from end of file using VIM

Ask Question Asked 9 years, 8 months ago Modified 3 years, 11 months ago Viewed 64k times  

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.

vim se-share-sheet#willShow s-popover:shown->se-share-sheet#didShow">Share Improve this question   edited Jun 7, 2013 at 11:49 user avatar Mat 196k4040 gold badges382382 silver badges396396 bronze badges asked Oct 12, 2012 at 21:48 user avatar Ransom Briggs 2,99533 gold badges3131 silver badges4646 bronze badges Add a comment se-share-sheet#willShow s-popover:shown->se-share-sheet#didShow">Share Improve this answer   edited Oct 28, 2013 at 21:06     answered Oct 12, 2012 at 21:51 user avatar David Brown 13k44 gold badges3838 silver badges5454 bronze badges
 

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 pattern XXXX

As ZyX mentioned this relies on wrapscan to be set, which it is by default. See :h 'wrapscan' for more information.