Bash 历史记录重新运行:避免使用 !bang! 的可能命令

发布于 2024-07-14 14:50:49 字数 671 浏览 9 评论 0原文

场景:

您正在执行日常 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌酱 2024-07-21 14:50:49

^r(即 CTLR-r),输入 foocommand 按 Enter ;)
这将搜索历史记录并显示最新的匹配条目

Press ^r (that's CTLR-r), type foocommand press Enter ;)
That will search history and show the latest matching entry

空城之時有危險 2024-07-21 14:50:49

你尝试过“!f”吗? 'f' 作为 foo 命令的第一个字母。

Did you try '!f'? 'f' as first letter of the foocommand.

若水微香 2024-07-21 14:50:49

就我个人而言,我使用从 ksh 派生的别名:(

alias r="fc -e -"

我想“r”代表“重新运行”...)

我可以重新运行以“foo”开头的最后一个命令:

r foo

我可以重新运行命令 46 到 50:

r 46 50

我可以重新运行命令585:

r 585

唯一的问题是,如果您输入:

$  cd somewhere

则运行“r cd”将不起作用,因为“cd”命令前面有一个额外的空格。 “fc”命令是历史记录的基础,您可以在重新运行命令 46 到 50 之前编辑它们,方法是键入:

fc 46 50

对于其他变体,依此类推。

(别名中的“-e -”表示“使用空编辑器进行编辑”;您可以编写“fc -e vim”来使用 vim 进行编辑,但大多数人设置 VISUAL 或 EDITOR 或 FCEDIT 或全部三个,以使其不必要。 )

对于其余的,作为一个“vim”人(狂热者?),我使用“set -o vim”,然后使用搜索工具:ESC并

/grep\ -e\ 'whatever

搜索包含“grep -e 'whatever”的命令的历史记录。 我可以重复搜索,在历史上进一步向后移动,或者在超调后反转方向,或者......我假设 emacs 模式具有等效的搜索机制。

Personally, I use an alias derived from ksh:

alias r="fc -e -"

('r' for 'rerun', I suppose...)

I can rerun the last command starting with 'foo':

r foo

I can rerun commands 46 to 50:

r 46 50

I can rerun just command 585:

r 585

The only gotcha is that if you typed:

$  cd somewhere

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:

fc 46 50

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

/grep\ -e\ 'whatever

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.

世界如花海般美丽 2024-07-21 14:50:49

该语法

!foo

将运行以 foo 开头的最后一个命令。

否则,bash 将使用 readline 库进行输入,该库支持可绑定到您选择的键的 history-search-forwardhistory-search-backward 命令。 我编辑了 ./inputrc 文件将它们绑定到 F8 和 Shift-F8,以便当我使用 PuTTY 连接时它们在控制台中的工作方式类似于 Windows 中的等效功能:

"\e[19~":history-search-backward
"\e[32~":history-search-forward

The syntax

!foo

will run the last command that began with foo.

Failing that, bash uses the readline library for input, which supports the history-search-forward and history-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:

"\e[19~":history-search-backward
"\e[32~":history-search-forward
り繁华旳梦境 2024-07-21 14:50:49

“查找历史记录并自动运行列表中的第一项”

好的。

`history | grep foocommand | head -1 | tr -s ' ' | cut -d ' ' -f 3-`

尝试一下,它的效果就像冠军一样!

"grep through history and automatically run the first item on the list"

Okay.

`history | grep foocommand | head -1 | tr -s ' ' | cut -d ' ' -f 3-`

Give it a try, it works like a champ!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文