How to jump to a specific heading in a man page?

6,565

Solution 1

From the command line for GNU man:

man --pager='less -p ^ENVIRONMENT' man

or for BSD man:

man -P 'less -p ^ENVIRONMENT' man

will jump to the "ENVIRONMENT" heading of the man page for man.

Here is a handy function:

mans () {    # Bash
    local pages string
    if [[ -n $2 ]]
    then
        pages=(${@:2})
        string="$1"
    else
        pages=$1
    fi
    # GNU man
    man ${2:+--pager="less -p \"$string\" -G"} ${pages[@]}
    # BSD man
    # man ${2:+-P "less -p \"$string\" -G"} ${pages[@]}
}

Examples:

Use normally:

mans bash

Go to the "DESCRIPTION" heading:

mans ^DESCRIPTION bash

Go to the "DESCRIPTION" heading of each man page in succession (press q and Enter to go to the next one):

mans ^DESCRIPTION bash ksh zsh

Go to the "Parameter Expansion" sub-heading (you can search for any string using regular expressions):

mans '^ *Parameter Expansion' bash

Search for the most recent regex you've used in Less:

mans '' bash

The match that you searched for won't be highlighted. If you'd prefer it to be, just remove the -G from the options to less.

This function makes no attempt to handle the other arguments and options that man supports.

Solution 2

I don't like the --pager/-P solution, because man might be used but not be called directly (e.g. when you use git help ...). So using an envvar is more flexible. But I find using PAGER='less ... kind of redundant, because less is usually the default pager anyway. You can use the LESS envvar to pass on parameters directly to less. This also has less quoting issues. E.g. this will correctly jump to the right section, even though it has a space in it:

LESS="-p file system" git help glossary

Solution 3

I use a simple trick to jump relatively fast between sections in man pages: I hit /^[A-Z] and then I can press n and N to jump forward and backwards.

The search regex utilizes the basic structure of typical man pages - The main sections are written in capital letters and they begin a line without indentation therefor they should appear at the beginning of lines.

Solution 4

You can use PAGER variable for this run to avoid BSD/GNU compatibility problem.

Use " quotes if the section title contains spaces:

PAGER='less -p ^"ENVIRONMENT"' man man

Solution 5

In man you can type / followed by a pattern to match e.g. to find the DEFAULT KEY BINDINGS section of the screen man page you would type

/^DEFAULT KEY BINDINGS
Share:
6,565

Related videos on Youtube

chiggsy
Author by

chiggsy

Updated on September 17, 2022

Comments

  • chiggsy
    chiggsy over 1 year

    I've set a filetype in vim for this. I want the help program to be man. Of course this does not work. For instance, with ssh_config's manpage, if I am on a word, say ServerAliveCountMax, I get an error, since there is no man pager for ServerAliveCountMax -- it's inside ssh_config's manpage.

    From the command line, is there any way to jump to a string or run some type of command inside man? Just like how info can take me to the Miscellaneous section of screen's info page:

    info screen Miscellaneous
    

    Is this possible with man? Even running a search would serve...


    For OSX/*BSD with /usr/bin/man, this works:

    man -P 'less -p PATTERN' ssh_config  
    
  • chiggsy
    chiggsy over 13 years
    Yes. This is true, but you have to be in man to do it. I want to get from the shell to that in one step.
  • chiggsy
    chiggsy over 13 years
    See my comment above, and actually, my question.
  • chiggsy
    chiggsy over 13 years
    promising, I edited my question, I see you ran that using either macports or linux, I have a -P option in the crappy BSD style option list... trying that.
  • chiggsy
    chiggsy over 13 years
    Thanks, I got it, but thanks again! BAH, I can't upvote your new answer again :(
  • Dennis Williamson
    Dennis Williamson over 13 years
    @Chiggsy: I take it the -P worked. If so, I'll add it to my answer.
  • Dennis Williamson
    Dennis Williamson over 13 years
    @Chiggsy: Should I add the -P BSD-style to my answer for future reference?
  • chiggsy
    chiggsy over 13 years
    Yes. It was definitely just dialect difference.
  • Patryk Obara
    Patryk Obara about 4 years
    Up to the top with this answer! Thanks :)