Find previous searches in less command
Asked Answered
B

2

22

In vim, if I am in search mode by pressing /, I can press Ctrl+p to find my last searched string, and press that combination again to find an even earlier searched string, and so on. I can do the same on commandline in emacs mode.

I want to do the same with less command (i.e., press /, and then press Ctrl+p to find what I searched for previously, and then press Enter to search that term again. Can I do that? How can I do that?

Edit: reworded to make the question clearer.

Biota answered 20/11, 2018 at 8:18 Comment(1)
@JamesBrown I think the OP wants to navigate within a file backwards via a search string rather than view a list of previously entered search strings. ? is the key to search backwards in lessSolis
C
44

To browse less search history (~/.lesshst by default, man less and LESSHISTFILE for more):

  1. less a file, for example less ~/.lesshst
  2. type / or ? to search forward or backward or & to show only matching lines (/!, ?! or &! for non-matching)
  3. use up and down arrows and to browse the history, press Enter to choose one
  4. n repeats the search, N in reverse direction. To reset the screen after showing only the matching lines with &, type & followed by Enter.
Chartist answered 20/11, 2018 at 12:25 Comment(2)
This is totally what I'm looking for. If I could, I'd select yours as the accepted answerSeneca
Why they decided N instead of p (stands for "previous")? Confused.Naturopathy
S
3

Use / to search forward. Use ? to search backward.

Use n for the next occurrence of the search item and N for prior occurrence. This works for both forward and backward searches.

And as pointed out in the comments if less history is enabled you can carry your search item across invocations of less.

man less and man lesskey for the details.

Solis answered 20/11, 2018 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.