What's the best way to search for the last occurrence of a string in less?

?string works for small files but doesn't seem to work with large or non-trivial syntax-highlighted files (eg, cat /var/log/cron | ccze -A | less -r), I had to go to the end of the file with G first then do a reverse search from there.

  • 3
    I think that's the way to do it in less.
    – sudodus
    Nov 4, 2021 at 12:43

1 Answer 1


You can search a string like always with /string but then just press Shift+ESC+f to get the last occurrence.


You can reverse search with N after typing your search /string.

Less only loads the visible part of the file and a little before and after it, so its remains quick for larger files but only searches in the part that is loaded.

If you want to search over the whole file you should use vim as editor or grep/sed/awk on the commandline.

Thanks @sudodus for pointing out my false answer.

  • 2
    This brings me to the end of the file in less 487-0.1
    – sudodus
    Nov 4, 2021 at 13:45
  • Yea you don't always necessarily open an actual file in less, it could have been piped eg cat /var/log/cron | ccze -A | less -r (added to the question now). <kbd>N</kbd> didn't find do a reverse search after /string, neither does <kbd>?</kbd> (<kbd>?</kbd> just after /string does a reverse search of string, difference being <kbd>N</kbd> does a search in the opposite direction).
    – sunnz
    Nov 5, 2021 at 11:32
  • I do want to page through the text so didn't want to use grep/sed/awk. Vim doesn't feel right and not sure if it can work with text piped from stdout, however I did try it with a log file eg vim -R /var/log/cron and yes reverse search is instant with syntax highlighting. That may do for most cases having said that in the spirit of the question which is about less, can it load the whole file like vim?
    – sunnz
    Nov 5, 2021 at 11:38
  • 1
    @sunnz, You can use the option -bn or --buffers=n that specifies the amount of buffer space less will use for each file, in units of kilobytes (1024 bytes). See man less. So it depends on the size of the file and the size of your RAM (how much is available), if you can load the whole file. (It would not be nice to start swapping.)
    – sudodus
    Nov 5, 2021 at 17:10
  • @sudodus I have tried that option like so: cat /var/log/cron | ccze -A | less -r -b-1 but it still requires me to <kbd>G</kbd> first, just doing a reverse search (ie ?string) straight away still doesn't work.
    – sunnz
    Nov 6, 2021 at 12:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .