1

This open-man-page-and-search-for-string-in-a-single-command question half covers what I'm trying to do:

Instead of doing man chmod and then /a+x to jump to the first section in the chmod man page that mentions a+x, I would like to know if there is a way to open the man page to a specific search string, similar to how you can do vi +string filename.txt in vi(m).

The chosen answer was

man foobar | less +/searched_string

However, when I try

man tmux | less +/^format

Less launches but returns

Pattern not found (press RETURN)

Why is the pattern not being found?


OS - Ubuntu 15.10

% less --version
less 458 (GNU regular expressions)
Copyright (C) 1984-2012 Mark Nudelman

2 Answers2

1

Use the environment variable LESS, overriding it for the single command you are running.

See also:

LESS='+/LESS[[:space:]]*Options' man less
LESS='+/\+cmd' man less
LESS='+/optional variable assignments' man bash
LESS=+/SIMPLE\ COMMAND\ EXPANSION man bash

I also discussed this more generally in a Meta post a while back:


There is one thing to keep in mind when creating the search pattern: If you choose a few consecutive words from the middle of a long paragraph, there is a chance that they won't be on the same line when that man page is viewed on a differently sized terminal. For example, the following works on a fullscreen terminal (on my monitor) but not on an 80-column terminal, because the words become split across lines:

LESS=+/allowing' a shell script' man bash

And because of the way man handles word spacing, the following is also unreliable across terminals:

LESS=+/"The latest version" man bash

The following two commands will get to the same locations, but more portably:

LESS=+/^INVOCATION man bash
LESS=+/^BUG man bash
Wildcard
  • 36,499
0

The problem was that when launching less and passing a search term as an option, the search will be case sensitive.
Therefore the solution is to use a search pattern using the same case. e.g. these work

man tmux | less +/^FORMAT

even better - to protect against shell expansion

man tmux | less '+/^FORMAT'

or alternatively

man tmux | less -p '^FORMAT'

Background

Whenever I used less/man searches were case insensitive, so I expected the same when passing a search argument at invocation.
It seems this is because, the version of man Ubuntu uses, opens less with the -i option making searches case insensitive.
So then when less is invoked without this option, you get less' default behaviour and suddenly less' searches become case sensitive.