How to Search Man Pages at the Command Line

Aug 27, 2016 - 10 Comments

Search man pages

Command line users are undoubtedly familiar with man pages, or manual pages, that contain details, help, and documentation to specified commands and functions. Referencing a man page can be essential when trying to learn proper syntax or how a command works, but with how large some manual pages are it can be a real drag to scroll through the entire man page to try and find a relevant portion. Additionally sometimes you just don’t know exactly which manual page you should look for relevant data for. Fortunately there are two search tools to search man pages and quickly find and access exactly what you’re looking for, whether finding and matching a string or search term in a currently active man page or by searching all manual pages for a match.

Searching man pages works the same in just about any unix based command line, whether it’s Mac OS, Linux, BSD, or whatever else. Here’s how it works:

How to Search All Man Pages for a String Match

If you want to find something about a general command, function, or feature, but you aren’t certain which man page the data will be in, or perhaps you just want to find all references to something, you an use a broad string match to search every single manual page on the computer for all matches:

man -K "String"

Note the flag is a capital -K, the string can be anything. For example, to find all manual pages that contain the string “eraseDisk” you’d use the syntax:

man -K "eraseDisk"
/usr/share/man/man3/Common Crypto.3cc? [ynq] n
/usr/share/man/man8/diskutil.8? [ynq] y

Hitting return will immediately start searching all manual pages found in /usr/share/man/* and when a match is found reports back, offering the matching manual page to you which you can accept with ‘y’ or dismiss with ‘n’ (or quit with ‘q’)

If you’re a Terminal.app user on the Mac, you’ll find the -K flag is similar to using the right-click search Man Index trick we discussed here, except it’s done entirely from the command line and requires no mouse or cursor interaction.

Search in Current Manual Page for Matches

Once you’re in a manual page, you may want to search within the currently open man page for a string match as well. That’s done with / like so:

/ search term

Let’s say we’re in the man page for launchd and you want to find matches for “LaunchAgents” within that manual page. Once you’re in man for launchd (man launchd) you’d use the following:

/launchagents

Any matches to the syntax in the current man page will be highlighted. You can then navigate between matches with n and shift+n.

Search man page for string match

The three tricks to remember to search once you’re within a man page are:

  • / search string – find matches to “search string” in current man page”
  • n – go to next match
  • shift + n – go to prior match

Remember these tips the next time you’re sorting through manual pages at the command line. And for those who use Terminal app, remember you can also search and launch manual pages from the Terminal Help menu directly, which would then allow you to use the aforementioned string search to look within a help doc to get further details.

Know of some other manual page search tricks? Let us know in the comments.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

10 Comments

» Comments RSS Feed

  1. Nathan says:

    I’ve used the free third-party app ManOpen for years. It has some cool features, including a GUI list of all man pages, apropos, an “Open Recent” menu, a standard Mac “Find” window to search the current man page, customizable appearance, etc. I don’t think it can replicate that man -K “String” command, so the command line is still needed for that. But ManOpen handles almost everything else beautifully.

    http://www.clindberg.org/projects/manopen.html

  2. rk says:

    how can I get such a beautiful black title bar?

    • Paul says:

      rk, in the screenshot of the Terminal pictured, the black title bar is implemented in the HyperTerm app https://hyperterm.org

      But yes I agree, the black title bar looks great. Hopefully some day we will get a universal dark theme for Mac OS, but for the time being you can only selectively get a black title bar in specific applications that implement it directly.

  3. Jan Steinman says:

    Eric mentioned apropos(1), which is indexed, and considerably faster than “man -K”. It also presents results in your chosen pager, instead of dumping it to standard out

    Your “chosen pager” is specified in the $PAGER environment variable. The default pager is more(1), but less(1) is faster and can search backwards using the “?” (instead of the noted “/”) command.

    To use less(1), simply put “export PAGER=less” in ~/.bash_profile (or the profile file of your default shell).

  4. Eric says:

    I always like your command line articles and just wanted to mention that the “apropos” command is often a good and quick way to find the man page that you are looking for. Thanks for the excellent web site!

  5. Scott Bayes says:

    Had a detailed response with a quick’n’dirty little ksh script to simplify man -[k|K] usage and cut down huge results lists, but your server went down and I lost it.

    Too. tired. to. retype. response.

    Here’s the script:

    bayesiMac: cat ~/bin/mank
    #!/bin/ksh

    if [[ $# -ne 2 ]];then
    echo Usage
    echo ” mank section search-text”
    echo ” e.g. mank 1 grep”
    exit
    fi

    man -k $2 | grep \($1\)

    • Paul says:

      Doh! Sorry about the comment loss, was it a temporary 503 error?

      • Scott Bayes says:

        Safari said it couldn’t find your server. 2 minutes later, it could.

        I almost always Select All and Copy before pressing a Submit button. Don’t know why I didn’t this time.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site