在 Linux 命令行中使用 Ctrl + R 反向查找/搜索历史命令
反向查找/搜索历史命令 - 反向查找/搜索执行过的命令
1. reverse-search-history (Ctrl + R)
Search backward starting at the current line and moving ‘up’ through the history as necessary. This is an incremental search. This command sets the region to the matched text and activates the mark.
从当前行开始向后搜索,并根据需要在历史记录中向上移动。这是一个增量搜索。此命令将区域设置为匹配的文本并激活标记。
1.
按下 Ctrl + R 组合键,进入反向搜索状态。
(base) yongqiang@yongqiang:~$
(reverse-i-search)`':
2.
输入查找字符串 git
,显示历史命令中的一条匹配命令。
为了提高查找效率,应该输入要查找命令中最特别的字符 (别的命令不包含的字符)。
(base) yongqiang@yongqiang:~$
(reverse-i-search)`git': git branch -a
3.
继续按下 Ctrl + R 组合键,可以继续向前搜索匹配命令。
多次按下 Ctrl + R 组合键,可以继续向前搜索匹配命令。
(base) yongqiang@yongqiang:~$
(reverse-i-search)`git': git diff
4.
按下 ->
键,退出搜索状态/交互模式。
(base) yongqiang@yongqiang:~$ git diff
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] 8.4.2 Commands For Manipulating The History, https://www.gnu.org/software/bash/manual/html_node/Commands-For-History.html