How do you run `less` and have it run a search pattern automatically?



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.


Posted 2010-08-04T16:29:46.933

Reputation: 4 761

1Awesome, 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 – 2010-08-19T02:37:35.240



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).


Posted 2010-08-04T16:29:46.933

Reputation: 2 100


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

less +/pattern

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

Matthew Flaschen

Posted 2010-08-04T16:29:46.933

Reputation: 2 370

2I 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 – 2015-07-28T18:26:09.553


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.

Adam Stewart

Posted 2010-08-04T16:29:46.933

Reputation: 141


You mean like?

less -p PATTERN filename

That is in the manual.


Posted 2010-08-04T16:29:46.933

Reputation: 50 788


There is indeed somthing in the less manpage.

You could try :

less -p<pattern>


less --pattern=<patern>


Posted 2010-08-04T16:29:46.933

Reputation: 932


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>

Dan Art

Posted 2010-08-04T16:29:46.933

Reputation: 1