使用制表符完成Bash历史记录扩展

Ala*_*aya 5 bash bash-completion

我经常使用命令历史扩展使用表单!n:m,其中n是历史命令m的编号,是参数的编号或其缩写形式.

有没有办法在现场扩展这些论点,以便我可以选项卡完成它们?

一个简单的例子:

$ mycmd long/path/with/plenty/of/subdirectories/
$ cp !$/tediously-verbose-filename.txt .
Run Code Online (Sandbox Code Playgroud)

我希望能够使用参数重复,而不必完全输入文件名(或使用ls或使用虚拟运行echo).

mkl*_*nt0 3

您正在寻找的概念术语是 [command]历史记录历史扩展- 如果您运行man bash,您将找到专门讨论此主题的部分HISTORY和。HISTORY EXPANSION

也就是说,在这种特殊情况下,它不是历史扩展,而是特殊的 shell 变量$_,它是你的朋友

它扩展到最近命令的最后一个(扩展的)参数

请尝试以下模拟您的场景的操作:

ls "$HOME"  

# Type this on the command line and press TAB (possibly twice)
# _before_ submitting to TAB-complete to matching files in "$HOME"
# (irrespective of what the current directory is).
# $_ at this point contains whatever "$HOME" expanded to, e.g. "/Users/jdoe".
cp $_/  
Run Code Online (Sandbox Code Playgroud)

注意:制表符补全是否适用于给定命令与$_是否使用无关。请参阅man bash部分Programmable Completion,了解如何手动启用感兴趣的命令的制表符补全功能。