Ama*_*ain 265 bash command-line keyboard-shortcuts input-history
我知道Esc+ .给你最后一个命令的最后一个参数.
但我对最后一个命令的第一个参数感兴趣.有关键绑定吗?
在同一行,有没有从最后一个命令获取第n个参数的通用方法?我知道,在bash脚本,您可以使用$0,$1等等,但这些并不在命令行工作.
那么,如何迭代前面命令的第0个参数,就像我们可以通过连续按Esc+ 来完成最后一个参数.?
use*_*081 292
!$ 获取上一个命令行参数的最后一个元素.
Pau*_*ce. 271
就像M-.(meta-dot或esc-dot或alt-dot)是readline函数一样yank-last-arg,M-C-y(meta-control-y或esc-ctrl-y或ctrl-alt-y)是readline函数yank-nth-arg.在没有指定的情况下n,它会猛拉上一个命令的第一个参数.
要指定参数,请按Escape和数字或按住Alt并按数字.你可以这样做Alt- -开始指定一个负数然后释放Alt并按下数字(这将从参数列表的末尾开始计算.
例:
输入以下命令
$ echo a b c d e f g
a b c d e f g
Run Code Online (Sandbox Code Playgroud)
现在在下一个提示符下键入echo(带有以下空格)
按Alt- Ctrl- y你现在会看到:
$ echo a
Run Code Online (Sandbox Code Playgroud)
没有按下Enter,请执行以下操作
按Alt- 3 Alt- Ctrl-y
按Alt- - 2 Alt- Ctrl-y
现在你会看到:
$ echo ace
Run Code Online (Sandbox Code Playgroud)
顺便说一句,您可以echo通过选择参数0来放置该行:
按Alt- 0 Alt- Ctrl-y
编辑:
要回答您添加到原文中的问题:
您可以按Alt- 0然后反复按Alt- .以逐步执行上一个命令(arg 0).同样Alt- -然后重复Alt- .将允许您逐步执行前面的倒数第二个参数.
如果历史记录中的特定行没有适当的参数,则响铃将响铃.
如果您经常使用特定组合,则可以定义一个宏,以便一次按键执行它.此示例将按Alt- Shift- 重新调用先前命令中的第二个参数Y.您可以选择您喜欢的任何可用按键而不是此按键.您可以反复按此按钮以逐步执行以前的操作.
要试用它,请在Bash提示符下输入宏:
bind '"\eY": "\e2\e."'
Run Code Online (Sandbox Code Playgroud)
要使其持久化,请将此行添加到您的~/.inputrc文件中:
"\eY": "\e2\e."
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎不适用于arg 0或负参数数字.
nop*_*ole 182
要使用第一个参数,您可以使用!^或!: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
Run Code Online (Sandbox Code Playgroud)
由于您的问题是关于使用任何其他参数,这里有一些有用的:
!^ 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
Run Code Online (Sandbox Code Playgroud)
前四种形式更常用.表单!:2-有点违反直觉,因为它不包括最后一个参数.
Aji*_*ony 51
我非常喜欢@larsmans的答案,我不得不学习更多.添加此答案可帮助其他人找到手册页部分,并知道要google的内容:
$ 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.
Run Code Online (Sandbox Code Playgroud)
Guy*_*atz 14
您还可以从历史记录中的任何命令获取参数!
$ 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
Mad*_*col 11
在 Linux 中,您可以重复命令以返回历史
最后一条命令是:
mv foo bar
Run Code Online (Sandbox Code Playgroud)
mvfoomv foo运行或将其添加到您的 ~/.zshrc
autoload -Uz copy-earlier-word
zle -N copy-earlier-word
bindkey "^[:" copy-earlier-word
Run Code Online (Sandbox Code Playgroud)
现在使用Alt+.返回任意值,然后使用Alt+:遍历参数
假设最后一个命令是
echo 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
54321echo来源:https : //stackoverflow.com/a/34861762/3163120
bind -lpbindkey -L我在这里保持最新状态:https : //github.com/madacol/docs/blob/master/bash-zsh_TerminalShorcuts.md
| 归档时间: |
|
| 查看次数: |
75443 次 |
| 最近记录: |