如何使用 Eg 显示 Linux 命令备忘单

学习 Linux 命令越来越容易了! 如果你知道如何 使用手册页 正确地,您已经完成了 Linux 命令行之旅的一半。 也有一些不错的 手册页替代品 可帮助您显示 Linux 命令备忘单。 与手册页不同,这些工具只会显示大多数命令的简明示例,而排除所有其他理论部分。 今天,让我们讨论这个列表的另一个有用的补充。 打招呼 例如,一个命令行备忘单工具,用于显示 Linux 命令的有用示例。

例如,为许多 Linux 和 Unix 命令提供了实际示例。 如果您想在不阅读冗长的手册页的情况下快速找到特定 Linux 命令的示例,例如是您的伴侣。 只需运行 eg 后跟命令的名称,然后在终端窗口中获​​取给定命令的简明示例。 就是这么简单!

eg 是一个免费的开源程序,用 Python 语言和代码在 GitHub 中免费提供。 对于那些想知道的人,例如来自 拉丁 单词 “典范的恩惠” 字面意思是“为了 example” 在英文中。Exempli Gratia 以其缩写而闻名 例如在英语国家。

在 Linux 中安装 Eg

例如可以使用安装 包管理器。 如果您的系统中没有 Pip,请按照以下链接中的说明进行安装。

  • 如何使用 PIP 管理 Python 包

安装 Pip 后,运行以下命令在您的 Linux 系统上进行安装:

$ pip install eg

使用 Eg 显示 Linux 命令备忘单

让我们从显示例如程序的帮助部分开始。 为此,请在没有任何选项的情况下运行 eg:

$ eg

样本输出:

usage: eg [-h] [-v] [-f CONFIG_FILE] [-e] [--examples-dir EXAMPLES_DIR]
          [-c CUSTOM_DIR] [-p PAGER_CMD] [-l] [--color] [-s] [--no-color]
          [program]

eg provides examples of common command usage.

positional arguments:
  program               The program for which to display examples.

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         Display version information about eg
  -f CONFIG_FILE, --config-file CONFIG_FILE
                        Path to the .egrc file, if it is not in the default
                        location.
  -e, --edit            Edit the custom examples for the given command. If
                        editor-cmd is not set in your .egrc and $VISUAL and
                        $EDITOR are not set, prints a message and does
                        nothing.
  --examples-dir EXAMPLES_DIR
                        The location to the examples/ dir that ships with eg
  -c CUSTOM_DIR, --custom-dir CUSTOM_DIR
                        Path to a directory containing user-defined examples.
  -p PAGER_CMD, --pager-cmd PAGER_CMD
                        String literal that will be invoked to page output.
  -l, --list            Show all the programs with eg entries.
  --color               Colorize output.
  -s, --squeeze         Show fewer blank lines in output.
  --no-color            Do not colorize output.

您也可以使用此命令带来帮助部分:

$ eg --help

现在让我们看看如何查看 example 命令用法。

要显示 Linux 命令的备忘单,对于 example grep, 跑:

$ eg grep

样本输出:

grep
 print all lines containing foo in input.txt
 grep "foo" input.txt
 print all lines matching the regex "^start" in input.txt
 grep -e "^start" input.txt
 print all lines containing bar by recursively searching a directory
 grep -r "bar" directory
 print all lines containing bar ignoring case
 grep -i "bAr" input.txt
 print 3 lines of context before and after each line matching "foo"
 grep -C 3 "foo" input.txt
 Basic Usage
 Search each line in input_file for a match against pattern and print
 matching lines:
 grep "<pattern>" <input_file>
[...]
使用 Eg 显示 Linux 命令备忘单

正如您在上面的输出中看到的,例如显示 example 命令 grep 实用程序以及简要说明。 无需阅读冗长的手册页,无需引用任何标志。 您将立即获得给定命令的示例。

您甚至还可以获得 eg 命令的示例:

$ eg eg

更改寻呼机

默认情况下,例如使用 less 如果整个输出不适合屏幕,pager 会逐页显示命令示例。 按 ENTER 键浏览后续页面中的所有示例。

您还可以使用不同的寻呼机 --pager-cmd 选项。 为了 example使用 cat 作为寻呼机,运行:

$ eg grep --pager-cmd=cat

这将在屏幕上的单个页面中显示整个输出。

要永久更改寻呼机,您需要在例如配置文件中设置寻呼机。

编辑例如配置文件:

$ nano ~/.egrc

笔记: 如果配置文件不存在,则创建它。

$ touch ~/.egrc

然后在文本编辑器中打开 eg 配置文件并在其中添加以下行:

[eg-config]
    --pager-cmd=cat

Save 文件和 close 它。 从现在开始,例如将使用 cat 命令作为寻呼机。

列出可用命令

例如项目的开发人员和所有其他贡献者为每个命令添加了许多示例。 您可以使用以下命令查看所有可用命令的列表:

$ eg --list

在编写本指南时,例如提供了 85 个命令行实用程序的示例。

$ eg --list | wc -l
85

编辑命令

如果您想在命令中添加更多示例,只需通过 -e 标志以编辑默认命令集并添加您自己的命令。

在添加/编辑自定义命令之前,创建一个目录来保存自定义命令。 这是您要保存所有自定义命令的目录。

$ mkdir ~/.eg/

接下来,编辑 eg 配置文件:

$ nano ~/.egrc

在其中添加以下行:

[eg-config]
    custom-dir = ~/.eg/

Save 文件和 close 它。 现在您可以编辑任何命令并添加您的自定义示例。

例如,要编辑 grep 命令示例,运行:

$ eg -e grep

这将打开您的默认编辑器。 添加示例并保存它们。 下次运行时,新添加的命令将显示在默认示例之前 eg grep 命令。

例如是高度可定制的。 您可以更改输出中的颜色,删除输出中的空行以及正则表达式替换等。这是一个示例 egrc 文件,其中指定了每个选项:

    [eg-config]
    # Lines starting with # are treated as comments
    examples-dir = /path/to/examples/dir
    custom-dir = /path/to/custom/dir
    color = true
    squeeze = true
    pager-cmd = 'less -R'

    [color]
    pound = 'x1b[30mx1b[1m'
    heading = 'x1b[38;5;172m'
    code="x1b[32mx1b[1m"
    prompt="x1b[36mx1b[1m"
    backticks="x1b[34mx1b[1m"
    pound_reset="x1b[0m"
    heading_reset="x1b[0m"
    code_reset="x1b[0m"
    prompt_reset="x1b[0m"
    backticks_reset="x1b[0m"

    [substitutions]
    # This will remove all four-space indents.
    remove-indents = ['^    ', '', True]

许多手册页没有提供 example 命令。 还有一些手册页很长。 例如,是手册页的不错替代品之一。 无需滚动浏览冗长的手册页,您可以快速找到 example 给定的命令立即。

由于没有命令命名 女士 在 Linux 中,你为什么不直接 别名 eg 对女人?

$ alias woman=eg
$ man grep
$ woman grep

有关更多详细信息,请参阅下面给出的 eg 实用程序的官方 GitHub 存储库。

资源: