我知道Esc +。给出最后一个命令的最后一个参数。

但我对最后一个命令的第一个参数感兴趣。 是否有一个键绑定来这样做?

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

同样,如果遍历前面命令的第0个参数,就像我们连续按Esc +对最后一个参数所做的那样,会怎么样呢?


当前回答

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

其他回答

^将得到第一个参数,!$将得到最后一个参数,!:n将得到第n个元素。

如果你使用的是mac,你会倾向于使用ctrl+字母来扩展字符。我在终端(iTerm2)中设置了空格右键选项键,并将其定义为meta。这意味着我使用键按字导航,并从以前的命令中提取参数。

我非常喜欢@larsmans的回答,所以我必须多学一些。添加这个 回答帮助其他人找到手册页部分,并知道要做什么 谷歌:

$ 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.

要粘贴第一个参数,请按住Alt键,当它向下时,按下'1'键和'。的关键。

要粘贴第n个参数,将上面的'1'键替换为相应的数字键。

如果这不起作用,您的终端模拟器可能在它到达shell之前捕获Alt键。一些终端(xfce4-terminal)允许关闭配置文件中的“Alt-”快捷键。

这要归功于Jonas Eberle,我从他的评论中找到了另一个答案。

要使用第一个参数,可以使用!^或!: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-有点违反直觉,因为它不包括最后一个参数。