Bash 历史记录重新运行:避免使用 !bang! 的可能命令
场景:
您正在执行日常 Bash shell 操作。 你想运行上一个命令,所以你输入:
history | grep foocommand
然后你会得到一个列表,其中包含你在历史记录中执行过的所有 foocommand
内容,列表如下:
585 foocommand --baz --bleet
750 foocommand | grep quux
987 history grep | foocommand
你决定你想要运行命令 585,因此您输入
!585
问题: 只是出于好奇,有没有办法在完成最后一步的同时仍然获得预期的结果? 如果有一种方式可以说:
"grep through history and automatically run the first item on the list"
或者
"grep through history and let me choose which item to run using the arrow keys"
Scenario:
You are doing your daily Bash shell stuff. You want to run a previous command so you type:
history | grep foocommand
Then you get a list of all the foocommand
stuff you did for however long your history has kept track, in a list like so:
585 foocommand --baz --bleet
750 foocommand | grep quux
987 history grep | foocommand
You decide you want to run command 585, so you type
!585
Question: Just for curiosity, is there a way to take that final step out of the equation and still get the expected outcome? It would be nice if there were a way to say:
"grep through history and automatically run the first item on the list"
or
"grep through history and let me choose which item to run using the arrow keys"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
按
^r
(即CTLR-r
),输入foocommand
按 Enter ;)这将搜索历史记录并显示最新的匹配条目
Press
^r
(that'sCTLR-r
), typefoocommand
press Enter ;)That will search history and show the latest matching entry
你尝试过“!f”吗? 'f' 作为 foo 命令的第一个字母。
Did you try '!f'? 'f' as first letter of the foocommand.
就我个人而言,我使用从 ksh 派生的别名:(
我想“r”代表“重新运行”...)
我可以重新运行以“foo”开头的最后一个命令:
我可以重新运行命令 46 到 50:
我可以重新运行命令585:
唯一的问题是,如果您输入:
则运行“r cd”将不起作用,因为“cd”命令前面有一个额外的空格。 “fc”命令是历史记录的基础,您可以在重新运行命令 46 到 50 之前编辑它们,方法是键入:
对于其他变体,依此类推。
(别名中的“-e -”表示“使用空编辑器进行编辑”;您可以编写“fc -e vim”来使用 vim 进行编辑,但大多数人设置 VISUAL 或 EDITOR 或 FCEDIT 或全部三个,以使其不必要。 )
对于其余的,作为一个“vim”人(狂热者?),我使用“set -o vim”,然后使用搜索工具:ESC并
搜索包含“grep -e 'whatever”的命令的历史记录。 我可以重复搜索,在历史上进一步向后移动,或者在超调后反转方向,或者......我假设 emacs 模式具有等效的搜索机制。
Personally, I use an alias derived from ksh:
('r' for 'rerun', I suppose...)
I can rerun the last command starting with 'foo':
I can rerun commands 46 to 50:
I can rerun just command 585:
The only gotcha is that if you typed:
then running 'r cd' won't work because there was an extra space in front of the 'cd' command. The 'fc' command underlies history, and you can edit commands 46 to 50 before rerunning them by typing:
and so on for the other variants.
(The '-e -' in the alias means 'edit with the null editor'; you could write 'fc -e vim' to edit with vim, but most people set VISUAL or EDITOR or FCEDIT or all three to make that unnecessary.)
For the rest, being a 'vim' person (fanatic?), I use 'set -o vim' and then the search facility: ESC and
to search history for a command containing "grep -e 'whatever". I can repeat the search, moving ever further backwards in history, or reverse direction after overshooting, or ... I assume that the emacs mode has an equivalent search mechanism.
该语法
将运行以
foo
开头的最后一个命令。否则,bash 将使用 readline 库进行输入,该库支持可绑定到您选择的键的
history-search-forward
和history-search-backward
命令。 我编辑了./inputrc
文件将它们绑定到 F8 和 Shift-F8,以便当我使用 PuTTY 连接时它们在控制台中的工作方式类似于 Windows 中的等效功能:The syntax
will run the last command that began with
foo
.Failing that, bash uses the readline library for input, which supports the
history-search-forward
andhistory-search-backward
commands that can be bound to keys of your choice. I've edited my./inputrc
file to bind them to F8 and Shift-F8, so that they work like Windows' equivalent feature in the console when I connect with PuTTY:好的。
尝试一下,它的效果就像冠军一样!
Okay.
Give it a try, it works like a champ!