linux复制历史记录,关于linux:使用bash历史记录获取上一个命令,复制它然后“运行”它,但是使用命令注释...

只是提高我的bash技能的问题。 我总是这样做:

$ history | grep some_long_command

...

...

123 some_long_command1.........

124 some_long_command2.........

...

然后,我可以通过执行命令运行命令:

!123

但是,我经常想这样做:

some_long_command1foobar

即 在运行之前更改命令。 您可以使用bash来运行此命令:

#some_long_command1

所以它得到了评论。

然后,我不必使用鼠标突出显示命令,编辑它然后运行它(我可以使用键盘 - 更快)。

我想我可以编写一个脚本来完成它,但可能已经在某个地方内置了功能......?

谢谢。

最简单的方法:将bind Space:magic-space放入bash配置文件中。 然后,按Space将!123扩展为some_long_command1。

@courteous:谢谢! 那个对我来说很新。 很有用。

我建议您使用ctrl+r而不是使用history命令,然后开始键入该命令。当你按下箭头键就像去修改它一样,它将退出自动完成识别,并允许你在运行前进行编辑。

更新:另外,如果你想循环包含你刚输入的字符串的不同命令,继续按ctrl+r

如果使用vi键绑定(set -o vi),还可以使用/搜索历史记录并进行编辑。

更强大的功能是允许键入部分命令或命令中的部分路径,然后使用箭头导航历史记录。搜索bash的history-search-backward,history-search-forward选项

DavidC,我见过其他人这样做,但它对我不起作用。当我输入命令的前几个字母时,我按下箭头,它忽略我输入的内容并浏览历史记录,好像我根本没有输入任何内容(从最新到最旧)。你如何配置你的shell来完成这个?

令人遗憾的是,向上/向下不会滚动匹配但会立即退出搜索。我希望它用向上/向下滚动匹配(表示我仍在寻找我想要的命令),并且只有左/右退出搜索(表示我找到了我想要的命令)现在我准备编辑它了。

@ADTC我刚刚找到了一种方法来做你和我想要的事情:按ctr+r,开始输入,然后当你想找到包含它的所有命令时,只需按ctrl+r即可。换句话说,你想用up / down做什么(以循环方式)用ctrl+r完成

@Miquel令人难以置信的是,搜索bash是如此简单。你救了我很多时间和命令!!

如果您错过了使用ctrl+r的命令,请不要忘记Ctrl+s

实际上,您只需将:p附加到命令即可打印它而无需实际运行它。例如:

$ ls -la

$ !!:p

将打印ls -la作为上一个命令而不运行它,你可以按↑(up)来查找并编辑它。

你也可以

!123:p

打印出123rd命令作为上一个命令。

+1最佳答案,因为它允许通常的历史命令来回答OP:!mycmd:p。如果你在Bash中打开了vi命令,那么Pursell的答案也同样好或者更好。如Miquel所提到的Ctrl-r是次佳选择。

如果123不在线的开头,你可以这样做:!?123?:p

您还可以尝试fc命令编辑历史记录中的命令。

WIKI说,

?fc? is a standard program on Unix that lists or edits and reexecutes,

commands previously entered to an interactive shell. fc is a built-in

command in the bash shell; help fc will show usage information.

除了反向增量搜索(Ctrl + R)之外,我们还有一些bash快捷方式:

来自man bash:

previous-history (C-p)

Fetch the previous command from the history list, moving back in the list.

next-history (C-n)

Fetch the next command from the history list, moving forward in the list.

beginning-of-history (M-&lt)

Move to the first line in the history.

end-of-history (M->)

Move to the end of the input history, i.e., the line currently being entered.

reverse-search-history (C-r)

Search backward starting at the current line and moving 'up' through the history as necessary. This is an incremental search.

forward-search-history (C-s)

Search forward starting at the current line and moving 'down' through the history as necessary. This is an incremental search.

non-incremental-reverse-search-history (M-p)

Search backward through the history starting at the current line using a non-incremental search for a string supplied by the user.

non-incremental-forward-search-history (M-n)

Search forward through the history using a non-incremental search for a string supplied by the user.

yank-nth-arg (M-C-y)

Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command. Once the argument n is computed, the argument is extracted as if the"!n" history expansion had been specified.

yank-last-arg (M-., M-_)

Insert the last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the"!$" history expansion had been specified.

shell-expand-line (M-C-e)

Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions. See HISTORY EXPANSION below for a description of history expansion.

history-expand-line (M-^)

Perform history expansion on the current line. See HISTORY EXPANSION below for a description of history expansion.

insert-last-argument (M-., M-_)

A synonym for yank-last-arg.

operate-and-get-next (C-o)

Accept the current line for execution and fetch the next line relative to the current line from the history for editing. Any argument is ignored.

edit-and-execute-command (C-xC-e)

Invoke an editor on the current command line, and execute the result as shell commands.

!123:gs/old/new/

将运行命令123用字符串'new'替换字符串'old'。

您可以通过按M- ^(mac上的option-shift-6)进入编辑模式。

输入:

!123M-^

你将编辑命令#123。这有点像使用ctrl-r,但从感叹号语法开始。

可以M-^重新绑定到手指更友好的东西吗?

而不是使用history命令,将history-search-backward / history-search-forward绑定到可以轻松记住的关键快捷键(我更喜欢PgUp / PgDown)。为此,请将其放入.inputrc文件中:

"":  history-search-backward

"":  history-search-forward

要获取,请在shell中键入Ctrl-V,并在输出的任何内容中将起始^[替换为\e。

设置完成后,您只需键入some并按PgUp即可获得some_long_command。如果您需要some_long_command with_some_arg但历史记录后面有一个类似的命令some_long_command with_some_other_arg,您可以循环直到通过键入some然后重复点击PgUp来达到它,或者您可以输入some,点击PgUp,将光标移动到两个命令开始不同的位置,键入几个字符并再次点击PgUp。这种快速浏览/区分类似命令的能力使我认为它比Ctrl-R更舒适。

你也可以

shopt -s histverify

在.bash_profile中,这会导致任何历史记录扩展在命令行上显示而不运行它,允许您在执行此操作之前进行编辑。

您可以尝试"建议框" - 类似历史https://github.com/dvorka/hstr - 它读取Bash历史记录并允许快速导航。

255626543688e6e2031aa863563536a2.png

要获取最后一个命令,只需键入hh,导航到该命令并使用向右箭头在命令行上获取它(您可以在其中编辑它和/或添加注释)。

别名r ='fc -s'

在你的.bashrc(家庭目录)

然后你可以输入

r

在命令提示符下,您将执行历史记录中最后一个命令(相同的参数)的副本。如果您觉得有必要,只需点击箭头即可查看您执行的操作。

^ p在unix / solaris中获取最后一个键入的命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值