37

Is there a way to search, while I'm typing in less? Just like the vim option, set incsearch.

As I didn't find a proper way to do it, is there any similar tool that can do it?

3
  • Why Don't you use vim as your pager!? Commented Sep 18, 2012 at 15:36
  • @user1146332 hmm, I don't know if VIM could handle a stream directly, but seems ire_and_curses has given the right way to use vim
    – daisy
    Commented Sep 18, 2012 at 15:48
  • With '--' as command line argument vim reads from stdin (posted from mobile phone) Commented Sep 18, 2012 at 16:07

4 Answers 4

58

You can do a search from the command line:

less -ppattern filename

Or, once inside less, use / followed by your pattern to do interactive searching (forwards). n and N repeat the search in the forward and reverse direction, respectively. That's the bare minimum you need to know; there are many more commands for more complex or specific searches.

Edit: To respond to your updated question, there's currently no way to do immediate incremental searching with less. Have you considered using view instead (opens Vim in read-only mode, so will use your incsearch setting)? Vim can be made even more pager-like with the vimpager script.

Some additional information: There is an open bug on the Ubuntu bug-tracker for incremental search support, but it doesn't look like it's going anywhere soon. Somebody has implemented incremental support on a github fork, but obviously you're going to have to compile a custom less to use that.

(And apart from the Ubuntu enhancement request there is currently (as of 2016-05-17) no such enhancement request on the official less bugtracker.)

3
  • Sorry, but the initial question i posted wasn't clear, can you check my updated question?
    – daisy
    Commented Sep 18, 2012 at 15:13
  • 1
    +1 for recommending view (read-only vi) - i hate it when people use vi instead of less or view, and lock the file unnecessarily.
    – cas
    Commented Sep 18, 2012 at 23:09
  • When I hit n, it adds n to the search pattern. Hitting enter terminates the search, after which hitting n has no effect. What to do? Commented Jun 3, 2019 at 2:02
3

It was implemented in https://github.com/gwsw/less/commit/b09a0c452d08afe2da8c9632d4fa768d6474b371 2 months ago, and hopefully will be coming to a distro near us!

1
less --incsearch filename

Added in this commit on January 13 2021.

0

http://greenwoodsoftware.com/less/

Version 598 was released for beta testing on 7 December 2021.

I add --incsearch to my LESS environment variable:

export LESS='--incsearch  --ignore-case --status-column --LONG-PROMPT --RAW-CONTROL-CHARS --HILITE-UNREAD --tabs=4 --no-init --window=2 '
    # Man-db passes extra options to the pager via the `LESS` environment variable,
    # which Less interprets in the same way as command line options.
    # The setting is hard-coded at compile time and starts with -i.
    # (The value is "-ix8RmPm%s$PM%s$" as of Man-db 2.6.2; the P…$ part is the prompt string.)


    # "--RAW-CONTROL-CHARS":   Get color support for 'less'
    # --no-init: This is sometimes  desirable if the deinitialization string does something unnecessary, like clearing the screen
    # --HILITE-UNREAD:  highlight first unread line after forward movement

    # https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/
source $HOME/dot_file/color_less_wf.zsh

These are the differences between version 590 and version 598:


Add the --header option.
Add the --no-number-headers option.
Add the --status-line option.
Add the --redraw-on-quit option.
Add the --search-options option.
Add 'H' color type to set color of header lines.
Add #version conditional to lesskey.
Add += syntax to variable section in lesskey files.
Allow option name in -- command to end with '=' in addition to '\n'.
Add $HOME/.config to possible locations of lesskey file.
Add $XDG_STATE_HOME and $HOME/.local/state to possible locations of history file.
Don't read or write history file in secure mode.
Fix display of multibyte and double-width chars in prompt.
Fix ESC-BACKSPACE command when BACKSPACE key does not send 0x08.
Add more \k codes to lesskey format.
Fix bug when empty file is modified while viewing it.
Fix bug when parsing a malformed lesskey file.
Fix bug scrolling history when --incsearch is set.
Fix buffer overflow when invoking lessecho with more than 63 -m/-n options.
Fix display bugs with --header.
Fix bug restoring color at end of highlighted text.
Fix bug in parsing lesskey file.

You must log in to answer this question.

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