17

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

jasonwryan
  • 73,126

3 Answers3

21

Try this trick:

man chmod | less +'/a\+x'

or

man chmod | more +'/a\+x'

With a backslash before the + sign because what comes after / is an extended regular expression.

  • 2
    Cool trick, I did not know that one! – terdon Oct 29 '13 at 20:22
  • But now, you do =) – Gilles Quénot Oct 29 '13 at 20:22
  • 5
    The -p switch obviates the need for the rather ungainly +/... – jasonwryan Oct 29 '13 at 20:26
  • Pretty nice! It would be interesting to hear how this works, I thought man already opened less (or whatever pager you have set). Is this piping less through less with a search initiated or is the pipe replacing the initial man-created instance of less? – Gregg Leventhal Oct 29 '13 at 20:39
  • @GreggLeventhal My guess is that man changes its behavior when it finds its output is no longer hooked to a tty device and most probably spits out plain text to standard output. Compare the formatting in man man to that in man man|less. – Joseph R. Oct 29 '13 at 21:09
  • 2
    @JosephR, no it's just that less (and most pagers) behaves like cat when its output is not a terminal. – Stéphane Chazelas Oct 29 '13 at 21:30
  • @jasonwryan, the +cmd comes from vi (I believe) where it also applies, so it's good to know. +/xxx is also available in other pagers like most or some implementations of more like the one found in util-linux – Stéphane Chazelas Oct 29 '13 at 21:37
  • 1
    If less is already your man pager, you can also optimise it by running LESS=+/searched_string man foobar. That also has the advantage of working with man -a – Stéphane Chazelas Oct 29 '13 at 21:41
  • @StephaneChazelas Thanks for the (typically) insightful pointer. – jasonwryan Oct 29 '13 at 21:50
0

Assuming that you use less as the pager (it's the default on modern systems), add the search instructions to the environment variable LESS. This is a list of command line options for less. You can pass a less command to execute, such as / to search, by prefixing the command with + (just like with vi).

LESS="$LESS +/a\+x" man chmod

The advantage over man chmod | less "+/a\+x" is that you don't lose the formatting.

See also zmv for zsh: Dry runs and man pages

0

Not as far as I know (but as @sputnick points out, I don't know much), but you can parse it:

man chmod | grep -C 5 'a+x'

I would recommend using a string that actually exists in the man page though, something like:

$ man chmod | grep -C 5 set-user-ID
   traversals.

SETUID AND SETGID BITS
   chmod  clears  the  set-group-ID  bit  of a regular file if the file's group ID does not match the
   user's effective group ID or one of the user's supplementary group IDs, unless the user has appro‐
   priate  privileges.   Additional  restrictions  may cause the set-user-ID and set-group-ID bits of
   MODE or RFILE to be ignored.  This behavior depends on the policy and functionality of the  under‐
   lying chmod system call.  When in doubt, check the underlying system behavior.

   chmod preserves a directory's set-user-ID and set-group-ID bits unless you explicitly specify oth‐
   erwise.  You can set or clear the bits with symbolic modes like u+s and g-s, and you can set  (but
   not clear) the bits with a numeric mode.

RESTRICTED DELETION FLAG OR STICKY BIT
   The  restricted  deletion  flag or sticky bit is a single bit, whose interpretation depends on the
terdon
  • 242,166