在 bash 中是否有重复第二个近似命令的快捷方式?

Zen*_*Zen 8 command-line bash command-history

我们都知道!!可以重复您在 bash 中执行的最后一个命令。

但有时我们需要做一些操作,比如

$ python test.py
$ vim test.py

$ python test.py # here is where I need to repeat the second proximate bash command
Run Code Online (Sandbox Code Playgroud)

我可以使用向上箭头键来做到这一点,但这需要我将右手移到一个不舒服的位置。所以我想知道是否有一个命令!!可以工作?

Mic*_*mer 5

您可以使用!-2

$ echo foo
foo
$ echo bar
bar
$ !-2
echo foo
foo
Run Code Online (Sandbox Code Playgroud)

这可能对您的右手情况没有帮助。

您还可以使用!string历史搜索来搜索此类案例:

$ python test.py
$ vim test.py
$ !py
python test.py # Printed, then run
Run Code Online (Sandbox Code Playgroud)

这可能更方便使用。它将运行:

历史列表中当前位置之前的最新命令,以string开头。

即使只是!p会工作。您可以使用!?string搜索整个命令行,而不仅仅是开始。