我知道Esc +。给出最后一个命令的最后一个参数。
但我对最后一个命令的第一个参数感兴趣。 是否有一个键绑定来这样做?
在同一行中,是否有从最后一个命令中获取第n个参数的通用方法? 我知道在bash脚本中,您可以使用$0,$1等,但这些在命令行上不起作用。
同样,如果遍历前面命令的第0个参数,就像我们连续按Esc +对最后一个参数所做的那样,会怎么样呢?
我知道Esc +。给出最后一个命令的最后一个参数。
但我对最后一个命令的第一个参数感兴趣。 是否有一个键绑定来这样做?
在同一行中,是否有从最后一个命令中获取第n个参数的通用方法? 我知道在bash脚本中,您可以使用$0,$1等,但这些在命令行上不起作用。
同样,如果遍历前面命令的第0个参数,就像我们连续按Esc +对最后一个参数所做的那样,会怎么样呢?
当前回答
如果你使用的是mac,你会倾向于使用ctrl+字母来扩展字符。我在终端(iTerm2)中设置了空格右键选项键,并将其定义为meta。这意味着我使用键按字导航,并从以前的命令中提取参数。
其他回答
在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
在接受的答案后面描述的方法对我来说也适用于第0个参数。在我的~/.inputrc中有这些行:
"\en": "\e0\e."
"\em": "\e1\e."
"\e,": "\e2\e."
\ e2 \ e。与\e2\e\C-y相比,它的优点是,如果重复按下它,它会循环执行前面的命令,而不是多次插入前面命令的第二个参数。
要插入之前的整个命令,可以输入!!\e^。\e^是history-expand-line。
要使用第一个参数,可以使用!^或!: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-有点违反直觉,因为它不包括最后一个参数。
我非常喜欢@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.
$获取上一个命令行参数的最后一个元素。