如何使用上一个命令的参数?

Ama*_*ain 265 bash command-line keyboard-shortcuts input-history

我知道Esc+ .给你最后一个命令的最后一个参数.

但我对最后一个命令的第一个参数感兴趣.有关键绑定吗?

在同一行,有没有从最后一个命令获取第n个参数的通用方法?我知道,在bash脚本,您可以使用$0,$1等等,但这些并不在命令行工作.

那么,如何迭代前面命令的第0个参数,就像我们可以通过连续按Esc+ 来完成最后一个参数.

use*_*081 292

!$ 获取上一个命令行参数的最后一个元素.

  • `!:3`让你成为第三个 (108认同)
  • !*给你所有的人 (97认同)
  • `!!`给你最后一个命令.如果您忘记使用`sudo`,这很有用. (67认同)
  • `!:1-2`可以获得除3个参数中的最后一个之外的所有参数 (38认同)
  • 只是一句警告 - 如果你使用像`echo foo {,bar} baz`这样的组合参数,那么命令会被记录为打印并随后展开.有了上面的内容,在解析为`echo foo {,bar}之后使用`echo!:1`然后扩展为`echo foo foobar` (4认同)
  • 另外,组合命令如'echo abc && echo!:2`仍然算作历史的一个命令,所以`echo foo && echo!:2bar`不会输出`foo` +`foobar`,而是`foo`和`(上一个命令的第二个参数)bar` (4认同)
  • `Alt + .`也获取前一个命令行参数的最后一个元素(在bash中) (3认同)
  • 有关更多信息,以上优秀评论的参考在这里:https://www.gnu.org/software/bash/manual/html_node/Event-Designators.html#Event-Designators (3认同)

Pau*_*ce. 271

就像M-.(meta-dot或esc-dot或alt-dot)是readline函数一样yank-last-arg,M-C-y(meta-control-y或esc-ctrl-y或ctrl-alt-y)是readline函数yank-nth-arg.在没有指定的情况下n,它会猛拉上一个命令的第一个参数.

要指定参数,请按Escape和数字或按住Alt并按数字.你可以这样做Alt- -开始指定一个负数然后释放Alt并按下数字(这将从参数列表的末尾开始计算.

例:

输入以下命令

$ echo a b c d e f g
a b c d e f g
Run Code Online (Sandbox Code Playgroud)

现在在下一个提示符下键入echo(带有以下空格)

Alt- Ctrl- y你现在会看到:

$ echo a
Run Code Online (Sandbox Code Playgroud)

没有按下Enter,请执行以下操作

Alt- 3 Alt- Ctrl-y

Alt- - 2 Alt- Ctrl-y

现在你会看到:

$ echo ace
Run Code Online (Sandbox Code Playgroud)

顺便说一句,您可以echo通过选择参数0来放置该行:

Alt- 0 Alt- Ctrl-y

编辑:

要回答您添加到原文中的问题:

您可以按Alt- 0然后反复按Alt- .以逐步执行上一个命令(arg 0).同样Alt- -然后重复Alt- .将允许您逐步执行前面的倒数第二个参数.

如果历史记录中的特定行没有适当的参数,则响铃将响铃.

如果您经常使用特定组合,则可以定义一个宏,以便一次按键执行它.此示例将按Alt- Shift- 重新调用先前命令中的第二个参数Y.您可以选择您喜欢的任何可用按键而不是此按键.您可以反复按此按钮以逐步执行以前的操作.

要试用它,请在Bash提示符下输入宏:

bind '"\eY": "\e2\e."'
Run Code Online (Sandbox Code Playgroud)

要使其持久化,请将此行添加到您的~/.inputrc文件中:

"\eY": "\e2\e."
Run Code Online (Sandbox Code Playgroud)

不幸的是,这似乎不适用于arg 0或负参数数字.

  • 在寻找bash/readline的键盘快捷键时,我喜欢运行```bind -lp```并查看当前的绑定. (13认同)
  • @ChadSkeeters:并且`-s`(Bash 4 中的新功能)列出了使用`-x` 创建的宏。 (2认同)

nop*_*ole 182

要使用第一个参数,您可以使用!^!:1

例:

$ echo a b c d e 
a b c d e
$ echo !^
echo a
a

$ echo a b c d e 
a b c d e
$ echo !:1
echo a
a
Run Code Online (Sandbox Code Playgroud)

由于您的问题是关于使用任何其他参数,这里有一些有用的:

!^      first argument
!$      last argument
!*      all arguments
!:2     second argument

!:2-3   second to third arguments
!:2-$   second to last arguments
!:2*    second to last arguments
!:2-    second to next to last arguments

!:0     the command
!!      repeat the previous line
Run Code Online (Sandbox Code Playgroud)

前四种形式更常用.表单!:2-有点违反直觉,因为它不包括最后一个参数.

  • 我非常喜欢这个答案,并且出于完整性考虑,我建议还添加一行内容来说明“!-2”语法,该语法使您可以访问上一个命令之前的命令。 (3认同)

Aji*_*ony 51

我非常喜欢@larsmans的答案,我不得不学习更多.添加此答案可帮助其他人找到手册页部分,并知道要google的内容:

$ man  -P 'less -p ^HISTORY\ EXPANSION' bash
<...>
Word Designators

Word designators are used to select desired words from the event.
A : separates the event specification from the word designator.
It may be omitted if the word designator begins with a ^, $, *, -,
or %.  Words are numbered from the beginning of the line, with the
first word being denoted by 0 (zero).  Words are inserted into the
current line separated by single spaces.

   0 (zero)
          The zeroth word.  For the shell, this is the command word.
   n      The nth word.
   ^      The first argument.  That is, word 1.
   $      The last argument.
   %      The word matched by the most recent ‘?string?’ search.
   x-y    A range of words; ‘-y’ abbreviates ‘0-y’.
   *      All of the words but the zeroth.
          This is a synonym for ‘1-$’.  
          It is not an error to use * if there is just one word in
          the event; the empty string is returned in that case.
   x*     Abbreviates x-$.
   x-     Abbreviates x-$ like x*, but omits the last word.

   If a word designator is supplied without an event
   specification, the previous command is used as the event.
Run Code Online (Sandbox Code Playgroud)


Orb*_*bit 18

!^可能是第一个参数的命令.我不确定是否有办法获得第n个.

  • 它是!:n如上所述 (6认同)

Guy*_*atz 14

您还可以从历史记录中的任何命令获取参数!


$ echo a b c d e f g
a b c d e f g
$ echo build/libs/jenkins-utils-all-0.1.jar
build/libs/jenkins-utils-all-0.1.jar
$ history | tail -5
  601  echo build/libs/jenkins-utils-all-0.1.jar
  602  history | tail -10
  603  echo a b c d e f g
  604  echo build/libs/jenkins-utils-all-0.1.jar
  605  history | tail -5
$ echo !-3:4
echo d
d
$ echo !604:1
echo build/libs/jenkins-utils-all-0.1.jar
build/libs/jenkins-utils-all-0.1.jar


Mad*_*col 11

在 Ubuntu 18.04 上测试


要插入先前的参数:

  • Alt+ .:插入最后一个命令的最后一个参数。
  • Alt+ #+ .: 从最后一个命令插入 #nth 最后一个参数。
  • Alt+ -, #, Alt+ ., zsh: Alt + -+ #+ .: 从最后一个命令插入第 #n 个参数。

在 Linux 中,您可以重复命令以返回历史

例子:

最后一条命令是:

mv foo bar
Run Code Online (Sandbox Code Playgroud)
  • Alt+ 0+ .: 插入最后一个命令的第一个参数 =mv
  • Alt+ 2+ .:插入最后一个命令的最后一个第二个参数 =foo
  • up, Ctrl+ w: 没有最后一个字的最后一个命令 =mv foo

一般快捷键

  • Ctrl+ w: 从光标中删除最后一个单词
  • Alt+ d: 从光标中删除下一个单词
  • Ctrl+ k: 剪切光标后的所有内容
  • Ctrl+ u, zsh: Alt + w: 剪切光标前的所有内容
  • zsh: Ctrl + u:剪切整个命令(在 bash 中你可以组合Ctrl+ u, Ctrl+ k
  • Ctrl+ y: 粘贴之前用Ctrl+uCtrl+剪切的字符k
  • Ctrl+ _:撤消上次编辑(超过Ctrl+时非常有用w
  • Ctrl+ left: 移到最后一个字
  • Ctrl+ right: 移动到下一个单词
  • homeCtrl+ a: 移至行首
  • endCtrl+ e: 移至行尾

遍历上一个命令中的参数

仅适用于 zsh

运行或将其添加到您的 ~/.zshrc

autoload -Uz copy-earlier-word
zle -N copy-earlier-word
bindkey "^[:" copy-earlier-word
Run Code Online (Sandbox Code Playgroud)

现在使用Alt+.返回任意值,然后使用Alt+:遍历参数

假设最后一个命令是

echo 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
  • Alt+ .:5
  • Alt+ .+ :4
  • Alt+ .+ :+ :3
  • Alt+ .+ :+ :+ :2
  • Alt+ .+ :+ :+ :+ :1
  • Alt+ .+ :+ :+ :+ :+ :echo

来源:https : //stackoverflow.com/a/34861762/3163120

查看所有可用的快捷方式

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

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

  • Mac 上的 Zsh 上的“Alt .”正在输入“≥” (3认同)
  • Esc键 。有效,即使它只适用于最后一个。你无法通过不断打字来回到过去。也许值得更新答案。非常感谢。 (2认同)

Mos*_*ner 6

!^ 将为您提供第一个参数, !$ 将为您提供最后一个参数, !: n将为您提供第 n 个元素。