用不同的参数重新运行上一个命令

noo*_*der 11 unix linux bash shell keyboard-shortcuts

如果要重新运行具有相同参数的命令,可以执行以下操作:

vim long_filename
cat !$                     #same as 'cat long_filename'
Run Code Online (Sandbox Code Playgroud)

这样可以节省在传递给它时再次键入前一个参数cat.

不过,我将如何传递的参数是不一样最后一次运行脚本/命令?

long_annoying_script_name arg1 arg2
? arg3 arg4                                  #? signifies shortcut symbols such as '!$'
Run Code Online (Sandbox Code Playgroud)

当然我可以按下"向上"箭头并删除参数并键入新的参数,但是有更短/更快的方式吗?

我不想分配别名.

Ry-*_*Ry- 10

!: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如果你发现输入更方便的话也有.

  • +1 - `!!:0 arg3 arg4` 文档的快捷方式:[Bash 历史单词指示符](https://www.gnu.org/software/bash/manual/html_node/Word-Designators.html#Word-Designators ) (2认同)

Mad*_*col 10

#TL;DR Alt+ 0+ .: 插入不带参数的最后一条命令


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


您可以组合键盘快捷键

让我们考虑最后一个命令是:

mv foo bar
Run Code Online (Sandbox Code Playgroud)

up, Ctrl+ w: 没有最后一个字的最后一个命令 =mv foo

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

一些有用的快捷键:

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

  • Alt+ number+ .:从最后一个命令中插入第 #n 个最后一个参数(重复以返回历史记录)

  • Alt+ -, number, Alt+ ., zsh: Alt + -+ #+ .: 从最后一个命令插入第 #n 个参数(重复以返回历史)

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

  • Ctrl+ w: 删除最后一个字

  • Alt+ d: 剪切下一个单词

  • Ctrl+ k: 剪切之后的所有内容

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

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

  • Ctrl+ y:粘贴以前用任何剪切命令剪切的字符。在 bash 中,您可以链接剪切命令,而Ctrl+y会将它们全部粘贴。

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

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

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

  • homeCtrl+ a: 移动到命令的开头

  • endCtrl+ e: 移至命令末尾

查看所有可用的快捷方式

  • 重击: bind -lp
  • zsh: bindkey -L

不幸的是有一些限制

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

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

例如: curl -I --header "Connection: Keep-Alive" /sf/ask/2672356011/

要使用+删除该网址,您必须重复 12 次。Ctrlw


有类似的快捷方式只会停在空格字符处会很棒


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

  • emacs 键绑定是默认设置。但是,如果更改为 vi 键绑定(在 bash 中,使用“set -o vi”,或通过修改 ~/.inputrc),则击键完全不同。 (3认同)
  • 这个答案假设 emacs 键绑定。 (2认同)