如何在 man 中查找所需信息

如何在 man 中查找所需信息

man命令为许多程序提供了很好的手册,但如何才能更有效地使用它呢?例如man gcc

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

但是如果我只需要其中的一部分怎么办?-Idir例如,跳转到所述部分的最佳方法是什么?

答案1

man通过按下/和搜索字符串来搜索页面

manless默认使用查看器。您可以less/(斜线)进行搜索,然后添加搜索字符串,例如-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

按下Enter键,获取第一个匹配项。

再次按下/(斜线) 和Enter键,重复操作直到找到所需的部分。您也可以按下 来n查找下一匹配项。

       -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)

按下即可h对可用的命令进行清晰的概览。

                   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

您可以在以下链接中找到有关“帮助工具”的更多详细信息

如何获取有关终端命令的帮助?

答案2

我无法改进Wildcard 的答案当我在 Unix 和 Linux 问答中问到同样的问题时。

如果您想要以连字符开头的模式的结果,请在指定的模式前使用grep。 是一个标志,大多数程序会将其解释为 “此后的任何内容都不应被视为标志”。使用的示例:man <program>----
man find

man find | grep -- -type

如果你需要更多信息,例如描述某个选项的整个部分,你可以可以尝试使用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.

但是,这并不适用于您可能搜索的每个选项。例如:

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

帮助不大。更糟糕的是,对于某些选项,你可能会误以为你已经阅读了有关该选项的整个文本,但实际上你并没有。例如,搜索-delete忽略了非常重要的警告,其中包含第二该标题下的段落。


我的建议是使用设置了环境变量的标准调用manLESS 我在这个网站的回答中经常使用它。

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

要详细了解其工作原理,请参阅:

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

如果您只想在手册页中快速、交互地找到选项,请学习使用 的less搜索功能。另请参阅:

答案3

当您查看手册页并通过按搜索时/less实际上是将您的搜索模式视为正则表达式当搜索命令行选项时,我发现\b在它们后面附加一个匹配项非常有用词边界这通常会跳过大量本来会匹配但不是我要找的文本(或者在任何情况下都不是我想要首先阅读的文本)。

例如,要搜索-I选项,您可以输入:

/-I\b
  • /字符告诉less您想要搜索,正如其他人提到的那样(参见MIB 的回答)。如果您希望在手册页中向上而不是向下进行搜索,则应写?而不是。/
  • -I是您要搜索的文字。
  • \b匹配单词字符(A-Za-z_)与非单词字符之间的边界,或单词字符与其出现的文本的开头或结尾之间的边界。

您可能仍需要查找后续匹配项。为此,请按n要返回前面的匹配项,请按Shift+ n

例如,在手册页-I中搜索时,我发现在查找实际记录了选项的匹配项之前,匹配了 6 次。相比之下,在查找匹配项之前,匹配了 1 次。gcc-I-I-I\b

如果您愿意,可以使用\>而不是\b\>仅匹配单词的结尾(其中“单词”是一个或多个单词字符,如上所定义)。如果您想匹配单词的开头,可以使用。但请注意,编写类似 的内容来匹配选项\<不起作用,因为无法匹配后跟 的空格。\<-I-I\<-

答案4

我想用最简单的方式来回答。

一旦您使用 打开软件包的手册页man <package>,您就可以使用 搜索实用程序来使用 查找特定选项的详细信息/<option>,例如,要检查 -r 选项的详细信息,请使用/-r

man页面使用vim键绑定,因此了解搜索键绑定vim非常有用。同样,info使用emacs键绑定。

相关内容