How to jump to a particular flag in a Unix manpage?

23

9

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.

dotancohen

Posted 2012-06-26T09:38:03.500

Reputation: 9 798

Answers

27

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.

akostadinov

Posted 2012-06-26T09:38:03.500

Reputation: 1 140

11

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.

ggll

Posted 2012-06-26T09:38:03.500

Reputation: 210

5

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

man -P 'less -p "     -o"' mount

rush

Posted 2012-06-26T09:38:03.500

Reputation: 957

Upvoted for being possible, but that is quite a bit more to type than to search from within man. Thanks, though! – dotancohen – 2012-06-26T12:35:11.777

That's pretty useful to send someone to the exact place in the man page :) – rush – 2012-06-26T12:38:06.667

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 – 2012-06-26T12:38:55.250

3

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

Jorge Israel Peña

Posted 2012-06-26T09:38:03.500

Reputation: 1 297

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.

rightfold

Posted 2012-06-26T09:38:03.500

Reputation: 579

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

Joe

Posted 2012-06-26T09:38:03.500

Reputation: 533

3You might want to try man:// in Konqueror. KDE has a manpage KIOSLAVE! – dotancohen – 2012-07-03T10:09:16.057

@dotancohen - Cool. Didn't know about that. – Joe – 2012-07-04T16:48:04.777

@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 – 2012-07-04T16:56:27.060

@dotancohen - Never mind - I looked up kioslaves on wikipedia. http://en.wikipedia.org/wiki/KIO

– Joe – 2012-07-04T17:06:42.730

Yeah, KDE has just about everything. Enjoy! – dotancohen – 2012-07-04T18:11:17.320