Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching from end of file using VIM

Tags:

vim

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.

like image 615
Ransom Briggs Avatar asked Oct 12 '12 21:10

Ransom Briggs


People also ask

How do I navigate to end of file in Vim?

Move cursor to end of file in vim In short press the Esc key and then press Shift + G to move cursor to end of file in vi or vim text editor under Linux and Unix-like systems.

How do I search last of file?

View Recent Files Using Windows Search If you want to see all the recent files on your system, Windows Search is the answer. Start by opening File Explorer to the top level folder you want to search. For example, selecting your Documents folder searches everything in that folder and all the subfolders it contains.

How do I search end of file in Linux?

Use the up arrow key to go backwards line by line or ctl+b to go page by page. This not only goes to the end of the file, it waits for additional output at the end, like tail -f . If this isn't what you want, you should use +G instead.


2 Answers

Type G$?XXXX

  • G to go to the end of the file
  • $ to go to the end of the line
  • ? to search backwards
  • XXXX 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.

like image 102
David Brown Avatar answered Oct 09 '22 02:10

David Brown


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.

like image 37
Peter Rincker Avatar answered Oct 09 '22 03:10

Peter Rincker