"bind -x" 可以访问当前命令行吗?

Can "bind -x" access the current command line?

当我输入一些命令时,有时我想阅读命令的帮助。例如,当我输入

sort --overwrite some_texI # I is a cursor

,我想检查 sort 命令是否有 --overwrite 选项,读取 sort --help 的输出或我所做的任何选项摘要。我希望 bind -x 可以实现这一点,但不知道如何传递(部分)当前命令行信息(在本例中为单词 "sort")。当然,bind -x 'KEY: "sort --help"' 效果很好,但我希望它适用于所有命令。所以伪命令将是 bind -x 'KEY: "CURRENT_COMMAND --help"'.

任何人都可以提供解决方案或提示吗?

您可以使用 bash 的 READLINE_LINE 变量。 man bash

bind [-m keymap] keyseq:readline-command

(snip)

        -x keyseq:shell-command

                (snip) When shell-command is executed, the shell sets the READLINE_LINE variable to the contents of the readline line buffer

所以你要的是READLINE_LINE的第一个字。因此,解决方案是 bind -x 'KEY": "array=($READLINE_LINE); ${array[0]} --help"'.