How do you run `less` and have it run a search pattern automatically?
Solution 1
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).
Solution 2
You can use + to send arbitrary commands. E.g.:
less +/pattern
-p PATTERN
(as posted by nik) is equivalent to +/PATTERN
.
Solution 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.
Solution 4
You mean like?
less -p PATTERN filename
That is in the manual.
Solution 5
There is indeed somthing in the less manpage.
You could try :
less -p<pattern>
or
less --pattern=<patern>
Related videos on Youtube
Neil
Updated on September 17, 2022Comments
-
Neil over 1 year
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.
-
Neil almost 14 yearsAwesome, 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.
-
-
DrStrangepork almost 9 yearsI 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, doless +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.