BASH - 如何使用从previous命令参数? [英] BASH - How to use arguments from previous command?
问题描述
我知道,ESC DOT给你的最后一个命令的最后一个参数。
I know that ESC DOT gives you the last argument of the last command.
但我感兴趣的最后一个命令的第一个参数。
是否有一个键绑定这样做呢?
But I'm interested in first argument of the last command. Is there a key binding to do so?
在同样的思路,有没有从最后一个命令获取第n个参数的通用方法。
(我知道,在bash脚本可以使用$ 0,$ 1等中,但这些都不在命令行工作)
On the same lines, is there a generic way of getting nth argument from last command. (I know that in a bash script you can use $0, $1 etc., but these don't work on commandline)
同时,怎么样通过previous命令的第0迭代的说法,就像我们可以用最后一个参数做通过不断pressing ESC DOT
推荐答案
正如 M -
(元点或ESC点或ALT点)是readline的功能抽出,最后ARG
, MCY
(元控制y或ESC-CTRL-Y或CTRL- ALT-y)是readline的功能抽出,第n-ARG
。如果不指定 N
,却是拷贝一previous命令的第一个参数。
Just as M-.
(meta-dot or esc-dot or alt-dot) is the readline function yank-last-arg
, M-C-y
(meta-control-y or esc-ctrl-y or ctrl-alt-y) is the readline function yank-nth-arg
. Without specifying n
, it yanks the first argument of the previous command.
要指定参数,preSS逃亡的数字或按住Alt键并preSS的数字。你可以做<大骨节病>替代骨节病> - <大骨节病> - 骨节病>开始指定然后负数松开Alt键和preSS数字(这将计算从参数列表的末尾
To specify an argument, press Escape and a number or hold Alt and press a number. You can do Alt--to begin specifying a negative number then release Alt and press the digit (this will count from the end of the list of arguments.
例如:
输入以下命令
$ echo a b c d e f g
a b c d e f g
现在在下一个提示符下,键入回声
(有以下空间),那么
Now at the next prompt, type echo
(with a following space), then
preSS <大骨节病>替代骨节病> - <大骨节病>控制骨节病> - <大骨节病>是骨节病>,现在您将看到:
Press Alt-Ctrl-y and you'll now see:
$ echo a
无pressing <大骨节病>输入骨节病>然而,请执行以下操作
preSS <大骨节病>替代骨节病> - <大骨节病> 3 骨节病> <大骨节病>替代骨节病> - <大骨节病>控制骨节病> - <大骨节病>是骨节病>
Press Alt-3 Alt-Ctrl-y
preSS <大骨节病>替代骨节病> - <大骨节病> - 骨节病> <大骨节病> 2 骨节病> <大骨节病>替代骨节病> - <大骨节病>控制骨节病> - <大骨节病>是骨节病>
Press Alt-- 2 Alt-Ctrl-y
现在您将看到:
$ echo ace
顺便说一句,你可能会选择把回声
就行参数0:
preSS <大骨节病>替代骨节病> - <大骨节病> 0 骨节病> <大骨节病>替代骨节病> - <大骨节病>控制骨节病> - <大骨节病>是骨节病>
Press Alt-0 Alt-Ctrl-y
编辑:
要回答你添加到你原来的问题:
To answer the question you added to your original:
您可以preSS <大骨节病>替代骨节病> - <大骨节病> 0 骨节病>然后反复preSS <大骨节病>替代骨节病> - 以通过previous命令步骤(ARG 0)。同样<大骨节病>替代骨节病> - <大骨节病> - 骨节病>然后重复<大骨节病>替代骨节病> - <大骨节病> 骨节病>将允许您以逐步previous下一页 - 。到最后一个参数。
You can press Alt-0 then repeatedly press Alt-. to step through the previous commands (arg 0). Similarly Alt-- then repeating Alt-. would allow you to step through the previous next-to-last arguments.
如果有在历史上在特定线路没有适当的参数,钟会响。
If there is no appropriate argument on a particular line in history, the bell will be rung.
如果有您经常使用的特定组合,可以定义一个宏这样一个按键将执行它。这个例子会记得从previous命令由pressing第二个参数<大骨节病>替代骨节病> - <大骨节病>移骨节病> - <大骨节病>是骨节病>。您可以自由选择preFER,而不是这一个任何可用的按键。您可以preSS反复按步previous的。
If there is a particular combination you use frequently, you can define a macro so one keystroke will perform it. This example will recall the second argument from previous commands by pressing Alt-Shift-Y. You could choose any available keystroke you prefer instead of this one. You can press it repeatedly to step through previous ones.
要尝试一下,在Bash提示符输入宏:
To try it out, enter the macro at a Bash prompt:
bind '"\eY": "\e2\e."'
要使它持久的,这行添加到您的〜/ .inputrc文件
文件:
To make it persistent, add this line to your ~/.inputrc
file:
"\eY": "\e2\e."
不幸的是,这似乎并没有给ARG 0或负值的参数号工作。
Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers.
这篇关于BASH - 如何使用从previous命令参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!