11

This question already has an answer here:

The man command brings up a nice manual for many programs, but how can I use it more effectively? For example man gcc brings:

NAME
       gcc - GNU project C and C++ compiler

SYNOPSIS
       gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-Wpedantic]
           [-Idir...] [-Ldir...]
           [-Dmacro[=defn]...] [-Umacro]
           [-foption...] [-mmachine-option...]
           [-o outfile] [@file] infile...

       Only the most useful options are listed here; see below for the
       remainder.  g++ accepts mostly the same options as gcc.
....
many text

But what if I need only some part of it? What is the best way to jump to the section where -Idir is described, for example?

marked as duplicate by muru, dessert, Eric Carvalho, fkraiem, vidarlo Jan 26 '18 at 11:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    you mustn't be a vi/vim user. it uses similar search functions as vi/vim. / to search, n for next etc... – guiverc Jan 7 '18 at 7:48
13

Search the man page by pressing / and the search string

man is using the viewer less by default. You can search in less by pressing / (slash), and then add the search string, in your example -Idir

GCC(1)                                GNU                               GCC(1)

NAME
       gcc - GNU project C and C++ compiler

SYNOPSIS
       gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-Wpedantic]
           [-Idir...] [-Ldir...]
           [-Dmacro[=defn]...] [-Umacro]
           [-foption...] [-mmachine-option...]
           [-o outfile] [@file] infile...

       Only the most useful options are listed here; see below for the
       remainder.  g++ accepts mostly the same options as gcc.

DESCRIPTION
       When you invoke GCC, it normally does preprocessing, compilation,
       assembly and linking.  The "overall options" allow you to stop this
       process at an intermediate stage.  For example, the -c option says not
       to run the linker.  Then the output consists of object files output by
       the assembler.

/-Idir

Press the Enter key, and get the first match.

Press / (slash) and the Enter key again, repeat until you find the section, that you want. You can also press n for the next match.

       -Idir
           Add the directory dir to the head of the list of directories to be
           searched for header files.  This can be used to override a system
           header file, substituting your own version, since these directories
           are searched before the system header file directories.  However,
           you should not use this option to add directories that contain
           vendor-supplied system header files (use -isystem for that).  If
           you use more than one -I option, the directories are scanned in
           left-to-right order; the standard system directories come after.

           If a standard system include directory, or a directory specified
           with -isystem, is also specified with -I, the -I option is ignored.
           The directory is still searched but as a system directory at its
           normal position in the system include chain.  This is to ensure
           that GCC's procedure to fix buggy system headers and the ordering
           for the "include_next" directive are not inadvertently changed.  If
           you really need to change the search order for system directories,
           use the -nostdinc and/or -isystem options.

       -iplugindir=dir
           Set the directory to search for plugins that are passed by
           -fplugin=name instead of -fplugin=path/name.so.  This option is not
           meant to be used by the user, but only passed by the driver.

 Manual page gcc(1) line 10179 (press h for help or q to quit)

Pressing h gives a nice overview over the available commands.

                   SUMMARY OF LESS COMMANDS

      Commands marked with * may be preceded by a number, N.
      Notes in parentheses indicate the behavior if N is given.
      A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.

  h  H                 Display this help.
  q  :q  Q  :Q  ZZ     Exit.
 ---------------------------------------------------------------------------

                           MOVING

  e  ^E  j  ^N  CR  *  Forward  one line   (or N lines).
  y  ^Y  k  ^K  ^P  *  Backward one line   (or N lines).
  f  ^F  ^V  SPACE  *  Forward  one window (or N lines).
  b  ^B  ESC-v      *  Backward one window (or N lines).
  z                 *  Forward  one window (and set window to N).
  w                 *  Backward one window (and set window to N).
  ESC-SPACE         *  Forward  one window, but don't stop at end-of-file.
  d  ^D             *  Forward  one half-window (and set half-window to N).
  u  ^U             *  Backward one half-window (and set half-window to N).
  ESC-)  RightArrow *  Left  one half screen width (or N positions).
  ESC-(  LeftArrow  *  Right one half screen width (or N positions).
  F                    Forward forever; like "tail -f".
  ESC-F                Like F but stop when search pattern is found.
  r  ^R  ^L            Repaint screen.
  R                    Repaint screen, discarding buffered input.
        ---------------------------------------------------
        Default "window" is the screen height.
        Default "half-window" is half of the screen height.
 ---------------------------------------------------------------------------

                          SEARCHING

  /pattern          *  Search forward for (N-th) matching line.
  ?pattern          *  Search backward for (N-th) matching line.
  n                 *  Repeat previous search (for N-th occurrence).
  N                 *  Repeat previous search in reverse direction.
  ESC-n             *  Repeat previous search, spanning files.
  ESC-N             *  Repeat previous search, reverse dir. & spanning files.
  ESC-u                Undo (toggle) search highlighting.
  &pattern          *  Display only matching lines
        ---------------------------------------------------
        A search pattern may be preceded by one or more of:
        ^N or !  Search for NON-matching lines.
        ^E or *  Search multiple files (pass thru END OF FILE).
        ^F or @  Start search at FIRST file (for /) or last file (for ?).
        ^K       Highlight matches, but don't move (KEEP position).
        ^R       Don't use REGULAR EXPRESSIONS.
 ---------------------------------------------------------------------------
HELP -- Press RETURN for more, or q when done

You find more details about 'help tools' at the following link

How can I get help on terminal commands?

  • @StevenPenny You are right, it goes to the beginning of the file. I guess I just want it to be previous... – Graipher Jan 7 '18 at 22:10
3

I can't improve on Wildcard's answer when I asked the same question at Unix & Linux Q&A.

If you want to grep the man <program> results for a pattern beginning with a hyphen, use -- before the pattern you specify. Example using man find :

man find | grep -- -type

If you want more info, for example the entire section describing an option, you could try using sed:

$ man find | sed -n '/-mindepth/,/^$/p'
   -mindepth levels
          Do  not apply any tests or actions at levels less than levels (a
          non-negative integer).  -mindepth  1  means  process  all  files
          except the command line arguments.

However, this won't work for every option you might search for. For example:

$ man find | sed -n '/^[[:space:]]*-type/,/^$/p'
   -type c
          File is of type c:

Not very helpful. Worse, for some options you could be misled into thinking you'd read the whole text about the option when you really hadn't. For example, searching -delete omits the very important WARNING contained as a second paragraph under that heading.


My recommendation is to use a standard call to man with the LESS environment variable set. I use it quite commonly in my answers on this site.

LESS='+/^[[:space:]]*-type' man find

To learn more about how this works, see:

LESS='+/^[[:space:]]*LESS ' man less
LESS='+/\+cmd' man less
LESS='+/\/' man less

If you just want to find the option quickly and interactively in the man page, learn to use less's search capabilities. And also see:

  • 1
    You may also want to use grep's option of -C followed by a number — number of lines of context to include with the matched line. Similarly see the options -A and -B. Without context, results of grep in a man page risk being not very useful. – Ruslan Jan 7 '18 at 17:02
2

I would like to answer it in simplest way.

Once you open manpage for the package using man <package>, you can use search utility to find the details of a particular options using /<option>, example, to check the details of -r option, use /-r.

2

When you view manual pages and search by pressing /, less is actually treating your search pattern as a regular expression. When searching for command-line options, I find it very useful to append \b to them, which matches a word boundary. This often skips over lots of text that would otherwise match but is not what I am looking for (or which is, in any case, not what I want to read first).

For example, to search for the -I option, you can type:

/-I\b
  • The / character tells less you want to search, as others have mentioned (see MIB's answer). You would write ? instead of / if you wanted your search to go upward in the manpage instead of downward.
  • -I is the literal text that you are searching for.
  • \b matches the boundary between a word character (A-Z, a-z, or _) and a non-word character, or between a word character and the very beginning or end of the text in which it occurs.

You may still need to find subsequent matches. To do that, press n. To go back to preceding matches, press Shift+n.

In the case of searching for -I in the gcc manpage, for example, I found that -I was matched six times prior to the match I was looking for where the -I option was actually documented. In contrast, -I\b was matched just once prior to that match.

If you prefer, you can use \> instead of \b. \> matches just the end of a word (where a "word" is one or more word characters as defined above). If you wanted to match the beginning of a word, you could use \<. Note, however, that it doesn't work to write something like \<-I to match the option -I, because \< would fail to match whitespace followed by -.

Not the answer you're looking for? Browse other questions tagged or ask your own question.