30

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
  • 11,720

8 Answers8

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.

akostadinov
  • 1,530
17

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
  • 270
5

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

man -P 'less -p "     -o"' mount
rush
  • 1,299
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

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

phuclv
  • 30,396
  • 15
  • 136
  • 260
Joe
  • 586
  • 6
  • 13
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).

shinokada
  • 2,715