29

When reading a Unix manpage in the terminal, how can I jump easily to the description of a particular flag?

For instance, I need to know the meaning of the -o flag for mount. I run man mount and want to jump to the place where -o is described. Currently, I search /-o however that option is mentioned in several places before the section that actually describes it, so I must jump around quite a bit.

Thanks.

8 Answers 8

33

What I do is put a few blank spaces in front of the flag like so:

/     -o

That's not 100% reliable but you jump through a much less hoops. If you want even better success rate, try "/^ +-o". That would find lines starting with blanks and followed by -o. I wouldn't like to type that weird string often though.

0
16

I have defined this function in my .bashrc

function manswitch () { man $1 | less -p "^ +$2"; }

which you can use as follows

manswitch grep -r

I got it from this commandlinefu.

Note: the argument to the -p switch of less is a regexp telling less to look for a line starting with (^) one or more spaces (+) followed by the switch (second arg. so $2), so it has the advantage of working with different formatting.

1
  • This is terrific. I've shortened the name from man switch to mans and thus men. Easier to type ))
    – dotancohen
    Mar 17, 2022 at 8:22
5

Also you can open the man page on specific position from command line with

man -P 'less -p "     -o"' mount
3
  • Upvoted for being possible, but that is quite a bit more to type than to search from within man. Thanks, though!
    – dotancohen
    Jun 26, 2012 at 12:35
  • That's pretty useful to send someone to the exact place in the man page :)
    – rush
    Jun 26, 2012 at 12:38
  • Yes, I was just thinking that if I had to open a man page in a script for some reason it would be useful as well. Thank you!
    – dotancohen
    Jun 26, 2012 at 12:38
5

@piccobello's answer is great, but it was eating the colors in my man pages. Instead of piping to less (since man already uses less by default usually), I simply pass the modified less command to man:

function manswitch() { man -P "less -p \"^ +$2\"" $1 }

This retains the functionality @piccobello had in his function, but retains colors.

2

When searching for flags, I have usually gone with akostadinov's answer, and typed / -flag in less, but this isn't usually accurate. To search for a flag with a more robust pattern without having to leave the pager, I've put a binding in my ~/.lesskey:

#command
\eF forw-search (\^\\s\+-|, -)-?
\ef noaction g\eF

These bindings go below the #command section (see man lesskey). Run lesskey to configure less to use the new bindings. These bindings cause alt-f to scroll the pager to the top of the file, then insert (^\s+-|, -)-? in the search prompt. This is a regex (see man re_format and Where is less search pattern reference?) which searches for flags in the typical format,

    -f, --flag ARGS
        Definition

Then, for example, you can type alt-f o enter to search for definitions for flags starting with -o or --o.

1
  • Very interesting, nice answer!
    – dotancohen
    Jul 15, 2022 at 12:17
1

I wrote a tool that does just this, called flagman. Still in development but already usable. For example:

$ ./flagman mount -o
       -o, --options opts
              Use the specified mount options.  The opts argument is a comma-separated list.  For example:

                     mount LABEL=mydisk -o noatime,nodev,nosuid


              For more details, see the FILESYSTEM-INDEPENDENT MOUNT OPTIONS and FILESYSTEM-SPECIFIC MOUNT OPTIONS sections.
1

The other solutions are quite good, but also remember that man pages are just data and you can easily do almost anything with them in Linux.

man some-command > file.txt

converts the page into a plain text file you can then manipulate. I keep a copy of the bash manual as text in my bin directory so I can just load it into my text editor to search for things and copy and paste while I'm editing scripts.

Or you can pipe it into filters such as

man some-command | grep -A lines-after "some pattern"

Although it wouldn't work for you in a terminal, I (with the help of a friend) even wrote a script that grabs a man page and displays it in a web browser so I can use its navigation/search features which are way better than less. It's a bit KDE dependent, but easy to modify.

http://dl.dropbox.com/u/54584985/kman

6
  • 3
    You might want to try man:// in Konqueror. KDE has a manpage KIOSLAVE!
    – dotancohen
    Jul 3, 2012 at 10:09
  • @dotancohen - Cool. Didn't know about that.
    – Joe
    Jul 4, 2012 at 16:48
  • @dotancohen -I just tried it in dolphin and it also works, but displays the man pages as directories and files and launches your default browser when you click on a file. I don't iunderstand the part about KIOSLAVE. I tried man://kioslave and it was the same as man://.
    – Joe
    Jul 4, 2012 at 16:56
  • @dotancohen - Never mind - I looked up kioslaves on wikipedia. en.wikipedia.org/wiki/KIO
    – Joe
    Jul 4, 2012 at 17:06
  • Yeah, KDE has just about everything. Enjoy!
    – dotancohen
    Jul 4, 2012 at 18:11
0

You can use the following script. This displays the option description or command description.

#!/usr/bin/env bash

########################
# Author: Shinichi Okada
# Version : v1.0.0
# Date: 2021-03-14
###########################

usage() {
    cat <<EOF
Name: manop
Description: This script outputs a command description or an option description from the man page.
Usage:  
    manop wget -b : Show wget -b option description
    manop ls: Show the ls command description
EOF
    exit 2
}

if [ $# -eq 0 ]; then
    usage
fi

if [ $# -eq 2 ]; then
    man "$1" | col -bx | sed -n "/^  *$2/,/^$/p"
else
    man "$1" | col -bx | sed -n "/^DESCRIPTION/,/^$/p"
fi

exit 0

On your terminal:

$ manop ls -h
       -h, --human-readable
              with -l and -s, print sizes like 1K 234M 2G etc.

$ manop  uniq
DESCRIPTION
       Filter  adjacent matching lines from INPUT (or standard input), writing
       to OUTPUT (or standard output).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .