在linux中以某些字符开头的重新加载历史命令的最快方法是什么?

ger*_*rry 2 linux shell

在Dos中我们可以输入前几个字符来过滤命令历史并快速找到合适的。但是如何在 Linux 中做同样的事情呢?

例如,当我测试本地服务器时:

cd 
sudo /etc/init.d/vsftpd start
wget ...
ls
emacs ...
sudo /etc/init.d/vsftpd restart
sudo /etc/init.d/vsftpd stop
...
Run Code Online (Sandbox Code Playgroud)

在 Dos 中,您可以sudo使用箭头键轻松键入并在以它开头的三个命令之间切换。但是在 Linux 中,下面的命令是我们能做的最好的吗?

historty | grep sudo
Run Code Online (Sandbox Code Playgroud)

我不喜欢它,因为历史很容易变得一团糟,而且还需要鼠标操作。

thk*_*ala 6

在 Bash(以及大多数类似的基于 readline 的应用程序)中,您可以按 调Ctrl-R出历史搜索功能:

$ date
Tue Feb  1 15:40:06 EET 2011
(reverse-i-search)`d': date
Run Code Online (Sandbox Code Playgroud)

Enter这里我得到:

$ date
Tue Feb  1 15:40:06 EET 2011
$ date
Tue Feb  1 15:40:52 EET 2011
Run Code Online (Sandbox Code Playgroud)

编辑:

您可以在此处查看与历史记录相关的 Bash 击键的完整列表。

您可以看到历史搜索键盘绑定的当前列表:

$ bind -P | grep search | grep history
forward-search-history can be found on "\C-s".
history-search-backward can be found on "\e[5~".
history-search-forward can be found on "\e[6~".
non-incremental-forward-search-history can be found on "\en".
non-incremental-forward-search-history-again is not bound to any keys
non-incremental-reverse-search-history can be found on "\ep".
non-incremental-reverse-search-history-again is not bound to any keys
reverse-search-history can be found on "\C-r".
Run Code Online (Sandbox Code Playgroud)

在我的情况下,Page-Up/Down 也可用于搜索以我已经键入的内容开头的命令,如我的配置/etc/inputrc

# Page Up/Down cycles through history only for matching entries
"\e[5~": history-search-backward       # Previous
"\e[6~": history-search-forward        # Next
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记提及通过反复按“CTRL-R”可以在列表中进行搜索。 (2认同)
  • @gerry:`Ctrl-S` 向前搜索,但您可能需要执行`stty -ixon` 来启用它(并禁用`Ctrl-S` 和`Ctrl-Q` 停止和启动数据流)。 (2认同)

小智 6

将以下内容放入您的~/.inputrc

"\e[A": history-search-backward
"\e[B": history-search-forward
Run Code Online (Sandbox Code Playgroud)

然后在键入后sudoUp将搜索以 开头的命令sudo

(您必须重新启动 bash。)(不确定这是否适用于 csh。)