18

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
  • 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. Commented Aug 19, 2010 at 2:37

6 Answers 6

12

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

18

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

less +/pattern 

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

1
  • 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. Commented Jul 28, 2015 at 18:26
3

You mean like?

less -p PATTERN filename

That is in the manual.

3

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.

2

There is indeed somthing in the less manpage.

You could try :

less -p<pattern> 

or

less --pattern=<patern> 
1

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> 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.