1

This question already has an answer here:

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

marked as duplicate by Gilles, cuonglm, jimmij, Scott, Anthon Apr 20 '16 at 5:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Uh? That does perform a regexp search. All that's missing in your command is the quotes so that the special characters aren't expanded by the shell: less '+/^\s*format' – Gilles Apr 19 '16 at 22:27
  • I edited the accepted answer to make this clearer. – Gilles Apr 19 '16 at 22:32
  • @Gilles you're right that the search is a regex, however the pattern ` man tmux | less +/^\s*format` doesnt work as intended. What I didnt realise is that the - search as an argument - method is case sensitive. So yes it is a duplicate, I didnt realise I was doing it wrong till after I asked - and answered the question! – the_velour_fog Apr 19 '16 at 22:36
  • Oh. less does case-sensitive search by default. But some versions of man invoke less -i to make search case-insensitive. – Gilles Apr 19 '16 at 22:38
  • 1
    If you edit the question to be about the case insensitivity issue, it wouldn't be a duplicate. But take care not to invalidate Wildcard's answer. – Gilles Apr 19 '16 at 22:39
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
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.

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