如何重复上一个命令的最后一部分?
我将 zsh 与 Robby Russell 的 oh-my-zsh 框架结合使用。如何创建快捷方式或其他内容来重复命令的最后部分?
例如,如果我输入:
mv something in/this/difficult/to/type/directory
有什么方法可以轻松获取此:in/this/difficult/to/type/directory
?
I'm using zsh with the oh-my-zsh framework of Robby Russell. How can I create a shortcut or something to repeat the last part of a command?
For example, if I type:
mv something in/this/difficult/to/type/directory
Is there any way to easily get this: in/this/difficult/to/type/directory
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
- sp3ctum,在评论此处
- sp3ctum, in comment here
.
也可以与zsh
和oh-my-zsh
一起使用。<esc>.
also works out of the box withzsh
andoh-my-zsh
.!$
为您提供上一个命令的最后一个参数。例子:
!$
gives you the last parameter of the previous command.Example:
我也遇到过这个问题 - 我总是使用
Alt.
在 bash 中插入最后一个单词。找到 oh-my-zsh 覆盖此的地方。在 lib/key-bindings.zsh 中,注释掉它,它应该像在 bash 中一样工作:
bindkey -s '\e.' “..\n”
I ran into this too - I've always used
Alt.
for insert-last-word in bash. Found where oh-my-zsh overrides this.In lib/key-bindings.zsh, comment out this and it should work like in bash:
bindkey -s '\e.' "..\n"
如果您来到这里寻找在 zsh 交互式 shell 上粘贴上一个命令中的最后一个单词,也许这就是您的答案。
任何 zsh 中都已经配置了默认快捷方式(或者如果您已经安装了 oh-my-zsh,我不太确定)。
要检查您是否安装了此快捷方式,请在绑定键中搜索insert-last-word
使用此键盘快捷方式时,您可以粘贴上一个命令中使用的最后一个单词。
例子:
If you get here looking for pasting last word from last command on an zsh interactive shell, maybe this is your answer.
There is a default shortcut already configured in any zsh (or maybe if you have installed oh-my-zsh, i'm not really sure).
To check if you have this shortcut installed, search in bind keys for insert-last-word
When using this keyboard shortcut, you can paste last word used in last command.
Example:
引用命令最后一个参数的另一种方法是使用
$_
上面的代码会将您移动到刚刚创建的目录。
$_
还可以做其他事情。参考:https://unix.stackexchange.com/questions/280453/understand-the-meaning-的Another way to reference the last argument of a command is with
$_
The above code will move you into the directory you just created.
$_
can also do other things. Reference: https://unix.stackexchange.com/questions/280453/understand-the-meaning-of只是为了扩展 @Charles Gueunet 的答案;
!!
- 重复整个上一个命令如果您忘记将
sudo
添加到命令的开头,这会很有用。简单的例子:Just to expand on @Charles Gueunet answer;
!!
- repeats the entire last commandThis is useful if you forgot to add
sudo
to the start of the command. Trivial example:无论您是在 bash 还是 zsh 中,您都可以使用
!
运算符来恢复上一个命令的参数:如果我们采用:
echo abc d
作为示例!$
- 最后一个参数:d!:*
- 所有参数: abc d(可以缩短!*
)!:1
- 第一个参数:a(与!^
相同)! :1-3
- 从第一个到第三个参数:ab c!:2-$
- 从第二个到最后一个参数:bc d这最后一点回答了你的问题,你可以接受命令的最后一部分。
注意:
!:0
是最后执行的命令,在我们的示例中为 echo相应的文档可以找到 在 gnu 网站 上,整个上下文提供了比此评论更多的资源。
Wether you are in bash or zsh, you can use the
!
operator to recover arguments of your previous command:If we take:
echo a b c d
as an example!$
- the last argument: d!:*
- all the arguments: a b c d (can be shorten!*
)!:1
- the first argument: a (same as!^
)!:1-3
- arguments from first to third: a b c!:2-$
- arguments from the second to the last one: b c dThis last point answer you question, you can take the last part of your command.
Note:
!:0
is the last command executed, here it would be echo in our exampleThe corresponding documentation can be found on the gnu website, the whole context gives much more resources than this comment.
我刚刚测试过,看来你可以像在 bash 中那样做:
!$
。I just tested and it seems you can do it the same way as in bash:
!$
.!*
为您提供上一个命令的全部 参数。例子:
!*
gives you ALL the arguments of the last command.Example: