I'm trying to run less in Linux, and I want it to search for something immediately after launch.

It's basically like doing this:

$ less

Then press '/', type a search pattern like "^commit \w+$", then press enter, and press 'n' to find each subsequent result.

I'd like less to be launched, and then search for a pattern. There doesn't seem to be anything in the man page about starting with a pattern, but perhaps you can send it commands like Vim.

  • 1
    Awesome, put this in your git config: core.pager = less -cFRX --pattern='^commit' and you'll be able to go to the next commit with the 'n' key right away. – Neil Aug 19 '10 at 2:37

From the man page:

   -ppattern or --pattern=pattern
          The  -p  option  on the command line is equivalent to specifying
          +/pattern; that is, it tells less to start at the  first  occur-
          rence of pattern in the file.

This works as expected using the latest version of less (436).

| improve this answer | |

You can use + to send arbitrary commands. E.g.:

less +/pattern

-p PATTERN (as posted by nik) is equivalent to +/PATTERN.

| improve this answer | |
  • 2
    I prefer this answer, because it is more complete. The option +/pattern is equivalent to --pattern=pattern, but it can do more, such as to start less at the end of a file, do less +G. The + option gives you everything -p|--pattern does plus the entire set of other commands to run in less, so I say don't bother remembering -p and just use +/ to search just as you would from inside less. – DrStrangepork Jul 28 '15 at 18:26

You mean like?

less -p PATTERN filename

That is in the manual.

| improve this answer | |

There are actually two ways to do this. As everyone else mentioned, you can use the -p/--pattern options:

less -p<pattern>
less --pattern=<pattern>

Your pattern will have to be wrapped in quotation marks since it contains a space.

However, there is actually a second way to do this:

LESS=-p<pattern> less
LESS=--pattern=<pattern> less

The second method has one distinct advantage. It can be used with other commands that use less for pagination!:

LESS=-p"^       read \[" man bash

This can quite literally be extended to search the bash man page for all builtin commands. I got a little carried away one day and "fixed" man for bash builtins.

| improve this answer | |

There is indeed somthing in the less manpage.

You could try :

less -p<pattern>


less --pattern=<patern>
| improve this answer | |

Another option is that when you run the less then type & and the pattern of your interest.

This way you can get only the lines that has your pattern of interest not all the lines. Meaning shows only the matching lines.

&/ <pattern_of_interest>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.