如何重复上一个命令的最后一部分?

发布于 2024-12-18 19:09:05 字数 307 浏览 3 评论 0原文

我将 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 技术交流群。

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

发布评论

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

评论(10

掩于岁月 2024-12-25 19:09:06

添加绑定键“\e”。将最后一个单词插入到您的 .zshrc

- sp3ctum,在评论此处

add bindkey '\e.' insert-last-word to your .zshrc

- sp3ctum, in comment here

残花月 2024-12-25 19:09:06

. 也可以与 zshoh-my-zsh 一起使用。

<esc>. also works out of the box with zsh and oh-my-zsh.

淡忘如思 2024-12-25 19:09:06

!$ 为您提供上一个命令的最后一个参数。

例子:

$ echo hello world
hello world
$ echo !$
echo world
world

!$ gives you the last parameter of the previous command.

Example:

$ echo hello world
hello world
$ echo !$
echo world
world
滿滿的愛 2024-12-25 19:09:06

我也遇到过这个问题 - 我总是使用 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"

躲猫猫 2024-12-25 19:09:06

如果您来到这里寻找在 zsh 交互式 shell 上粘贴上一个命令中的最后一个单词,也许这就是您的答案。

任何 zsh 中都已经配置了默认快捷方式(或者如果您已经安装了 oh-my-zsh,我不太确定)。
要检查您是否安装了此快捷方式,请在绑定键中搜索insert-last-word

$ bindkey -L | grep insert

...
bindkey "^[." insert-last-word
bindkey "^[_" insert-last-word
...

使用此键盘快捷方式时,您可以粘贴上一个命令中使用的最后一个单词。
例子:

$   echo a b c d e f g
a b c d e f g
...

$ 
[use shortcut]
$ g

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

$ bindkey -L | grep insert

...
bindkey "^[." insert-last-word
bindkey "^[_" insert-last-word
...

When using this keyboard shortcut, you can paste last word used in last command.
Example:

$   echo a b c d e f g
a b c d e f g
...

$ 
[use shortcut]
$ g
梦回旧景 2024-12-25 19:09:06

引用命令最后一个参数的另一种方法是使用 $_

mkdir 'a_new_directory'
cd $_

上面的代码会将您移动到刚刚创建的目录。

$_ 还可以做其他事情。参考:https://unix.stackexchange.com/questions/280453/understand-the-meaning-的

Another way to reference the last argument of a command is with $_

mkdir 'a_new_directory'
cd $_

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

不羁少年 2024-12-25 19:09:06

只是为了扩展 @Charles Gueunet 的答案;

  • !! - 重复整个上一个命令

如果您忘记将 sudo 添加到命令的开头,这会很有用。简单的例子:

$ cat /root/file
Permission denied
$ sudo !!
here's the conent

Just to expand on @Charles Gueunet answer;

  • !! - repeats the entire last command

This is useful if you forgot to add sudo to the start of the command. Trivial example:

$ cat /root/file
Permission denied
$ sudo !!
here's the conent
各自安好 2024-12-25 19:09:05

无论您是在 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 d

This 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 example

The corresponding documentation can be found on the gnu website, the whole context gives much more resources than this comment.

飘然心甜 2024-12-25 19:09:05

我刚刚测试过,看来你可以像在 bash 中那样做:!$

I just tested and it seems you can do it the same way as in bash: !$.

瀟灑尐姊 2024-12-25 19:09:05

!* 为您提供上一个命令的全部 参数。

例子:

% echo hello world  
hello world

% echo !*  
(expands to)-> % echo hello world
hello world

!* gives you ALL the arguments of the last command.

Example:

% echo hello world  
hello world

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