yuk*_*say 12 bash bash-expansion
我发现自己<command> --help | grep <feature>每天都在做非常非常频繁的事情。我想知道是否有可能使类似的东西^^扩展到"--help | grep"然后我这样做:
ls ^^ size
Run Code Online (Sandbox Code Playgroud)
这将执行以下操作:
ls --help | grep size
Run Code Online (Sandbox Code Playgroud)
tgw*_*tdt 17
您可以为此使用 bash 函数:
将以下内容放入您的 ~/.bashrc 中:
qh() {
type -all "$1" ; { man "$1" || "$1" --help ;} | egrep -i -- "$2"
}
Run Code Online (Sandbox Code Playgroud)
当您保存您的bashrc do 时,source ~/.bashrc您可以执行以下操作:
$ qh ls size
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'--block-size=M' prints sizes in units of
-h, --human-readable with -l and/or -s, print human readable sizes
-s, --size print the allocated size of each file, in blocks
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
Run Code Online (Sandbox Code Playgroud)
Sté*_*las 15
使用zsh,您将使用全局别名:
$ alias -g '^^=--help|grep --color -i'
$ ls ^^ size
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'--block-size=M' prints sizes in units of
1,048,576 bytes; see SIZE format below
-h, --human-readable with -l and/or -s, print human readable sizes
-s, --size print the allocated size of each file, in blocks
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
The SIZE argument is an integer and optional unit (example: 10K is 10*1024)
Run Code Online (Sandbox Code Playgroud)
使用bash,您可以使用历史扩展,这是在 shell 语法解析中足够早发生的扩展,它可以替代管道:
用你想要替换的文本和一个你不太可能使用的特殊字符来填充历史记录(就像£这里碰巧在我的键盘上):
$ --help $(: £)|grep
bash: --help: command not found
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
Run Code Online (Sandbox Code Playgroud)然后使用历史扩展来检索:
$ ls !?£? size
ls --help $(: £)|grep size
--block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'--block-size=M' prints sizes in units of
-h, --human-readable with -l and/or -s, print human readable sizes
-s, --size print the allocated size of each file, in blocks
-S sort by file size, largest first
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
Run Code Online (Sandbox Code Playgroud)或者您可以readline扩展--help|grep某些键或键序列按下。为了bash仅适用于(而不是其他应用程序,例如gdb使用 readline),您可以使用bindbash 内置命令,它是bash配置的 API readline,例如在您的~/.bashrc:
bind '"^^": "--help|grep "'
Run Code Online (Sandbox Code Playgroud)
或者添加到您的~/.inputrc(readline 的配置文件)中:
$if Bash
"^^": "--help|grep "
$endif
Run Code Online (Sandbox Code Playgroud)
(还有其他类似rc或es使用 readline 的shell ,在那里进行绑定可能有意义,但是 AFAICT,它们rl_readline_name在调用之前不会设置变量,readline因此您将无法$if为它们添加一些语句(它们会other像所有应用程序一样显示)使用 readline 而不告诉它他们的应用程序名称))。
请注意,您需要^在第一个之后的半秒内(默认情况下)输入第二个以进行替换。
您可以使用 readline 绑定:
添加一行
"^^": "--help | grep "
Run Code Online (Sandbox Code Playgroud)
到你的 ~/.inputrc
然后在您的术语中按 ^X ^R,绑定将被激活。
键控ls ^^现在将导致ls --help | grep.
小智 5
less查看帮助信息您可能会发现查看与您的搜索查询匹配的行的周围上下文很有用。
hh () { "${1}" --help | less -p "${2}" ; }
Run Code Online (Sandbox Code Playgroud)
调用此bash函数的语法类似于qh@tgwtdt 的答案中的函数,第一个参数是要检查的命令,第二个参数是搜索词。例如:
hh ls size
Run Code Online (Sandbox Code Playgroud)
hh ls "symbolic link"
Run Code Online (Sandbox Code Playgroud)
这将打开 中的完整帮助消息less,突出显示搜索词的每个实例,并滚动到搜索词的第一个实例。然后您可以按n向前滚动到包含搜索词的下一行,n再次滚动到下一行,依此类推。要回滚到上一个实例,请按N。使用Home,End,Page Up,Page Down,Up Arrow,和Down Arrow一般的导航键。按q或Q退出less并返回到命令行。