本文来自stackoverflow一个问答,提及的技巧似乎以前在网上或资料上未见过,特译之。
原文地址:http://stackoverflow.com/questions/4009412/bash-how-to-use-arguments-from-previous-command
译文:
问:
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?
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)
Also, what about iterating through 0th argument of previous commands, like we can do with last argument by continuously pressing ESC DOT
我知道ESC-.(即ALT-.)可以引用上条命名的最后一个参数。
但如果感兴趣的是第一个参数,是否有一个快捷键来引用呢?
在同一行,是否有一个一般的方法来获得上条命令的第N条参数(我知道bash脚本中你可以使用$0,$1...,但在命令行下不起作用)
此外,如果是像ESC-.(ALT-.)那样引用上条命令的最后一个参数,怎样去逐条访问每条过去命令的第0个参数呢?
答:
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.
就像 M-.(meta-.,或esc-.或alt-.)是调用readline函数 yank-last-arg一样,M-C-y(meta-ctrl-y或esc-ctrl-y或alt-ctrl-y)是调用readline函数yank-nth-arg。不指定n,函数黏贴(yank)过去命令的第一个参数。
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.
为了指定一个参数,可以按Esc然后接一个数字或同时按住alt和一个数字。你可以按alt--来开始指定一个负数,然后松开alt再按一个数字,这将从过去命令的参数的从后向前计数的参数。
Example:
例子:
Enter the following command
输入下面命令
$ 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 Alt-Ctrl-y and you'll now see:
现在在下个提示符下,输入echo(跟随一个空格),接着按alt-ctrl-y然后你可以看到:
$ echo a
without pressing Enter yet, do the following
不回车,接着做:
Press Alt-3 Alt-Ctrl-y
Press Alt-- 2 Alt-Ctrl-y
Now you will see:
现在你看到:
$ echo ace
By the way, you could have put the echo
on the line by selecting argument 0:
顺便说,你可以把像引用参数0一样输入echo:
Press Alt-0 Alt-Ctrl-y
现在回答你的补充问题:
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.
你能按alt-o然后重复的按alt-.来访问过去每一个命令,类似的,alt--然后alt-.允许你访问过去命令的每一个倒数第二个参数。
If there is no appropriate argument on a particular line in history, the bell will be rung.
如果历史命令上没有恰当的参数,将响铃提示.
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.
如果有一个你经常使用的特定的组合,你可以定义一个宏,这样可以一键执行。比如下面的例子可以重复调用过去命令的第二个参数通过按alt-shift-y.你可以选择任何你喜欢的按键。你可以重复按来访问过去的参数。
To try it out, enter the macro at a Bash prompt:
现在来试试,在bash提示符下输入宏:
bind '"\eY": "\e2\e."'
To make it persistent, add this line to your
~/.inputrc
file:
如果要永久化,则把下面的这行加入你的~/.inputrc文件:
"\eY": "\e2\e."
Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers
不幸的是,这似乎对参数0或负参数不起作用。