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

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

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

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


当前回答

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

其他回答

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

您还可以从历史记录中的任何命令获取参数! $ echo a b c d f g A b c d e f g $ echo build/libs/jenkins-utils-all-0.1.jar 建立/ libs / jenkins-utils-all-0.1.jar $ history | tail -5 601 echo build/libs/jenkins-utils-all-0.1.jar 602历史|尾巴-10 603回声a b c d e f g 604 echo build/libs/jenkins-utils-all-0.1.jar 605历史|尾巴-5 $ echo !-3:4 回声d d $ echo !604:1 构建/ libs / jenkins-utils-all-0.1.jar回响 建立/ libs / jenkins-utils-all-0.1.jar

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

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

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

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

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

在Ubuntu 18.04上测试


插入先前的参数:

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

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

例子:

最后一个命令是:

mv foo bar

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+u和Ctrl+k剪切的字符 Ctrl+_:撤销上次编辑(当超过Ctrl+w时非常有用) Ctrl+左:移动到最后一个单词 Ctrl+右:移动到下一个单词 home或Ctrl+a:移动到行首 end或Ctrl+e:移到行尾

要遍历前面命令中的参数

只适用于ZSH

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

autoload -Uz copy-earlier-word
zle -N copy-earlier-word
bindkey "^[:" copy-earlier-word

现在使用Alt+。,然后使用Alt+:遍历参数

假设最后一个命令是

echo 1 2 3 4 5

替代+.: 5 Alt+.+:: 4 Alt+.+:+:: 3 替代+.+:+:+: 2 其他+.+:+:+:+: 1 Alt+.+:+:+:+:: 回声

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

查看所有可用的快捷方式

Bash: bind -lp zsh: bindkey -L


我在这里保持最新消息:https://github.com/madacol/knowledge/blob/master/bash-zsh_TerminalShorcuts.md