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

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

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

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


当前回答

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

其他回答

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

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

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

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

在接受的答案后面描述的方法对我来说也适用于第0个参数。在我的~/.inputrc中有这些行:

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

\ e2 \ e。与\e2\e\C-y相比,它的优点是,如果重复按下它,它会循环执行前面的命令,而不是多次插入前面命令的第二个参数。

要插入之前的整个命令,可以输入!!\e^。\e^是history-expand-line。