Re 运行 上一个具有不同参数的命令

Re run previous command with different arguments

如果您想重新 运行 具有相同参数的命令,您可以这样做:

vim long_filename
cat !$                     #same as 'cat long_filename'

这省去了在传递给 cat 时再次输入前一个参数的麻烦。

但是,我如何将不相同的参数传递给最后一个运行script/command ?

long_annoying_script_name arg1 arg2
? arg3 arg4                                  #? signifies shortcut symbols such as '!$'

当然我可以只按 'up' 箭头并删除参数并输入新参数,但是有 shorter/faster 方法吗?

我不想分配别名。

!:0 应该可以解决问题。来自 zsh 文档:

   Word Designators
       A word designator indicates which word or words of a given command line
       are to be included in a history reference.  A `:' usually separates the
       event specification from the word designator.  It may be  omitted  only
       if  the  word designator begins with a `^', `$', `*', `-' or `%'.  Word
       designators include:

       0      The first input word (command).
       n      The nth argument.
       ^      The first argument.  That is, 1.
       $      The last argument.
       %      The word matched by (the most recent) ?str search.
       x-y    A range of words; x defaults to 0.
       *      All the arguments, or a null value if there are none.
       x*     Abbreviates `x-$'.
       x-     Like `x*' but omitting word $.

(它也适用于 bash。)如果您觉得输入起来更方便,还有 !-1

#TL;DR Alt+0+.: 插入没有参数的最后一个命令


在 Ubuntu 18.04 上使用默认键绑定设置测试 (即 Emacs 键绑定)


您可以组合键盘快捷键

让我们将最后一个命令视为:

mv foo bar

up , Ctrl+w: last command without the last word = mv foo

Alt+0+.: 最后一个命令的第一个参数 = mv

一些有用的快捷方式:

  • Alt+.:插入最后一个命令的最后一个参数(重复返回在历史上)

  • Alt+number+.: 最后插入#nth来自上一个命令的参数 (重复历史记录)

  • Alt+- , number , Alt+., zsh: Alt+- +#+.: 插入来自上一个命令的#nth 第一个参数(重复返回历史)

  • 剪切命令(相对于光标位置)

  • Ctrl+w: 剪切最后一个字

  • Alt+d: 切下一个字

  • Ctrl+k: 剪切

    之后的所有内容
  • Ctrl+u,zsh:Alt+w: 剪切之前的所有内容

  • zsh: Ctrl+u: 剪切整个命令(在 bash 你可以组合 Ctrl+u , Ctrl +k)

  • Ctrl+y: 粘贴先前用任何剪切命令剪切的字符. 在bash中你可以链接cut命令Ctrl+y 将全部粘贴。

  • Ctrl+_:撤消上次编辑(超过[=95时非常有用=]Ctrl+w)

  • Ctrl+:移动到最后一个字

  • Ctrl+:移动到下一个单词

  • home or Ctrl+a: 移动到开始命令

  • end or Ctrl+e: 移动到末尾命令

查看所有可用的快捷方式

  • bash: bind -lp
  • zsh: bindkey -L

遗憾的是有一些限制

“单词”仅包含 a-zA-Z 个字符,因此任何符号字符都会停止单词快捷方式。

所以如果最后一个参数是 url 并且你想用 Ctrl+w 删除它,它将是痛.

例如:curl -I --header "Connection: Keep-Alive"

要使用 Ctrl+w 擦除 url,您d 必须重复 12 次。


如果有类似的快捷方式只在 space 字符处停止就好了


我在这里保持最新状态:https://github.com/madacol/docs/blob/master/bash-zsh_TerminalShorcuts.md