我知道 Esc + 。给你最后一个命令的最后一个参数。
但我对最后一个命令的第一个参数感兴趣。是否有键绑定可以这样做?
在同一行中,是否有从最后一个命令获取第 n 个参数的通用方法?我知道在 bash 脚本中,您可以使用 $0
、$1
等,但这些在命令行上不起作用。
另外,如何遍历先前命令的第 0 个参数,就像我们可以通过连续按 Esc + 来处理最后一个参数一样?
!$
获取前一个命令行参数的最后一个元素。
正如 M-.
(meta-dot 或 esc-dot 或 alt-dot)是 readline 函数 yank-last-arg
,M-C-y
(meta-control-y 或 esc-ctrl-y 或 ctrl-alt-y)是读取线函数 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
编辑:
要回答您添加到原件中的问题:
您可以按 Alt-0,然后反复按 Alt-。单步执行前面的命令 (arg 0)。同样 Alt-- 然后重复 Alt-。将允许您逐步完成之前的倒数第二个参数。
如果历史上的某一行没有合适的论据,就会敲响警钟。
如果您经常使用某个特定的组合,您可以定义一个宏,这样一键即可执行它。此示例将通过按 Alt-Shift-Y 调用先前命令中的第二个参数。您可以选择任何您喜欢的可用按键来代替这个按键。您可以反复按它以逐步浏览以前的内容。
要试用它,请在 Bash 提示符下输入宏:
bind '"\eY": "\e2\e."'
要使其持久化,请将此行添加到您的 ~/.inputrc
文件中:
"\eY": "\e2\e."
不幸的是,这似乎不适用于 arg 0 或负参数数。
bind -lp
并查看当前的绑定。
-s
(Bash 4 中的新功能)列出了使用 -x
创建的宏。
Alt-3
Alt-.
(, Alt-.
, ...) (循环第三个参数)是我多年来一直在寻找的
要使用第一个参数,您可以使用 !^
或 !: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-
形式有点违反直觉,因为它不包括最后一个参数。
mv file1 file2 file3 target/
获得 file3
?
!:-2
可以为您提供直到第二个参数的所有内容。在这种情况下 echo a b
。
我非常喜欢@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 个。
!:n
,但从末尾而不是从开头开始计数?
您还可以从历史记录中的任何命令中获取参数!
$ 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
在 Ubuntu 18.04 上测试
要插入先前的参数:
Alt+.:插入最后一个命令的最后一个参数。
Alt+#+.:从最后一个命令插入第 #n 个最后一个参数。
Alt+- , # , Alt+., zsh: Alt+-+#+.: 从最后一个命令插入#nth第一个参数。
在 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+left:移动到最后一个单词
Ctrl+right:移动到下一个单词
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
Alt+.: 5
Alt+.+:: 4
Alt+.+:+:: 3
Alt+.+:+:+:: 2
Alt+.+:+:+:+:: 1
Alt+.+:+:+:+:+:: 回声
来源:https://stackoverflow.com/a/34861762/3163120
查看所有可用的快捷方式
重击:绑定-lp
zsh: 绑定键 -L
我在这里保持最新状态:https://github.com/madacol/knowledge/blob/master/bash-zsh_TerminalShorcuts.md
Alt .
正在输入 ≥
🤔
bindkey -L
来查看所有绑定的快捷方式,也许这将有助于查明问题,甚至可以找到其他有用的命令。以我为例:绑定 Alt .
定义为 bindkey "^[." insert-last-word
。 Ctrl L
(清除屏幕)-> bindkey "^L" clear-screen
。 Alt+Ctrl h
-> bindkey "^[^H" backward-kill-word
(与 Ctrl w
相同以删除最后一个单词)
bindkey "^[^_" copy-prev-word
!^ 将获得第一个参数,!$ 将获得最后一个参数,!:n 将获得第 n 个元素。
基本上,它可用于提取先前的(命令的)参数。
例如,如果发出以下命令:
echo Hello, world how are you today?
那么,Hello,
将是第一个参数,而 today?
第六个,即最后一个参数;这意味着可以通过键入以下内容来引用它:
Alt+6 后跟 Ctrl-Alt-6
Ctrl 传统上表示为键名前面的帽子字符 ^
,而 Alt 表示为 M-
,即 M< /strong>eta 前缀。
所以上面的快捷方式可以重新定义为 ^My
以进行 yank。
此外,命令行中有帽子替换快捷方式:
echo Hello, world!
^Hello^Bye
Bye, world!
替换上一个命令的第一个匹配字符串,意思是:
Hello, world! Hello, people!
^Hello^Bye
会导致:
Bye, world! Hello, people!
保持第二个匹配 (hello
) 不变。
注意:帽子之间不要留空隙,否则操作将不起作用。
以上只是一个快捷方式:
!:s/Hello/Bye
event-level(*) 替换上一个命令中第一个找到(匹配)的字符串,而在第一部分前面加上 g
开关将适用于整行 g局部:
echo Hello, world! Hello, people!
!:gs/Hello/Bye
Bye, world! Bye, people!
通常在其他相关命令中执行,例如 sed
、vi
和 regex
(正则表达式) - 一种标准的搜索方式(匹配字符串)。
不,你不能在这里做 !:sg/Hello/Bye 或 !:s/Hello/Bye/g ,这就是语法!
!用于活动;事件可以理解为命令历史中的命令输出或操作。
这就是我自己使用它并从我从各种来源(包括手册页、博客和论坛)中阅读的内容中自己尝试的东西所理解的。
希望它能揭示 bash
的神秘方式,即 Bourne-Again shell(在 sh
shell 上的一个游戏,它本身以其发明者的姓氏后称为 Bourne shell),在包括服务器在内的许多发行版中是默认 shell (服务器操作系统的)。
接受答案末尾描述的方法也适用于我的第零个参数。我的 ~/.inputrc
中有这些行:
"\en": "\e0\e."
"\em": "\e1\e."
"\e,": "\e2\e."
\e2\e.
与 \e2\e\C-y
相比的优势在于,如果重复按下它而不是多次插入前一个命令的第二个参数,它会循环执行前一个命令。
要插入整个前面的命令,您可以键入 !!\e^
。 \e^
是 history-expand-line
。
如果你在 Mac 上,你会倾向于使用 ctrl+letter 获得扩展字符。我在终端(iTerm2)设置中将我的空格键选项键定义为元。这意味着我使用该键按单词导航并从以前的命令中提取参数。
要粘贴第 1 个参数,请按住 Alt 键,然后在按下时按“1”键,然后按“。”钥匙。
要粘贴第 n 个参数,请将上面的“1”键替换为相应的数字键。
如果这不起作用,您的终端仿真器可能会在它进入 shell 之前捕获 Alt 键。某些终端(xfce4-terminal)允许关闭配置文件中的“Alt-”快捷方式。
感谢 Jonas Eberle,我已经从他的评论中找出了这个,这里是另一个答案。
!:3
让您获得第三个!!
为您获取整个最后一个命令。如果您忘记使用sudo
,则很有用。!:1-2
为您提供所有参数,但 3 个参数中的最后一个除外echo foo{,bar} baz
这样的组合参数,该命令将被记录为打印并随后扩展。有了上面,使用echo !:1
解析为echo foo{,bar}
然后扩展为echo foo foobar