首页 文章

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

提问于
浏览
231

我知道Esc . 为您提供最后一个命令的最后一个参数 .

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

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

Also, what about iterating through the 0th argument of previous commands, like we can do with the last argument by continuously pressing Esc + .?

9 回答

  • 47

    正如 M-. (meta-dot或esc-dot或alt-dot)是readline函数 yank-last-argM-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
    

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

    按Alt-Ctrl-y,您现在将看到:

    $ echo a
    

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

    按Alt-3 Alt-Ctrl-y

    按Alt - 2 Alt-Ctrl-y

    现在你会看到:

    $ echo ace
    

    顺便说一句,您可以通过选择参数0将 echo 放在行上:

    按Alt-0 Alt-Ctrl-y

    Edit:

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

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

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

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

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

    bind '"\eY": "\e2\e."'
    

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

    "\eY": "\e2\e."
    

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

  • 252

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

  • 11

    要使用第一个参数,可以使用 !^!: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
    

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

    !^      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
    

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

  • 164

    我非常喜欢@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.
    
  • 20

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

  • 2

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

    $ 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
    
  • 0

    基本上它在yanking previous (命令) arguments 中有用 .

    例如,如果发出以下命令:

    echo Hello, world how are you today?
    

    那么, Hello, 将是第一个参数, today?sixth ,这是最后一个参数;意思是可以通过输入来引用它:

    Alt 6后跟Ctrl-Alt-6


    Ctrl传统上表示为前缀为键名称的帽子字符 ^ ,而Alt表示为 M- ,即 M eta前缀 .

    因此,上面的快捷方式可以重新定义为 ^My 到yank .


    此外,命令行中还有hats替换快捷方式:

    echo Hello, world!
    
    ^Hello^Bye
    
    Bye, world!
    

    替换上一个命令的 first matched string ,意思是:

    Hello, world! Hello, people!
    
    ^Hello^Bye
    

    会导致:

    Bye, world! Hello, people!
    

    留下第二场比赛( hello )不变 .

    注意:不要在帽子之间留出空间,否则操作无效 .


    以上只是一个捷径:

    !:s/Hello/Bye
    

    事件级别(*)替换上一个命令中第一个找到(匹配)的字符串,而为第一个部分添加 g 开关前缀将适用于 the whole line g lobally:

    echo Hello, world! Hello, people!
    
    !:gs/Hello/Bye
    
    Bye, world! Bye, people!
    

    通常在其他相关命令中完成,例如 sedviregex (正则表达式) - 一种非常有效的搜索方式(匹配字符串) .

    不,你不能做!:sg / Hello / Bye或!:s / Hello / Bye / g这里,这就是语法!


    • !是为了事件;事件可以理解为命令输出或在命令中完成的操作历史 .

    这就是我自己使用它并通过我从各种来源(包括手册页,博客和论坛)阅读的内容自行尝试的理解 .

    希望它能够揭开_Ber-Again shell(在 sh shell上的一个游戏,其发明者的姓氏后称为Bourne shell)的神秘方式,包括服务器(服务器操作系统)在内的许多发行版中的默认shell .

  • 0

    在接受的答案结尾处描述的方法也适用于我的第零个论点 . 我在 ~/.inputrc 中有这些行:

    "\en": "\e0\e."
    "\em": "\e1\e."
    "\e,": "\e2\e."
    

    \e2\e. 优于 \e2\e\C-y ,如果反复按下它而不是多次插入前一个命令的第二个参数,它会循环显示前面的命令 .

    要插入整个上一个命令,可以键入 !!\e^ . \e^history-expand-line .

  • 240

    !^会给你第一个参数,!$会给你最后一个参数,!:n会得到你的第n个元素 .

相关问题