翻译_bash:bash命令行下如何引用历史命令的参数
2014-11-24 12:36
316 查看
本文来自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
or esc-dot or alt-dot) is the readline function
or esc-ctrl-y or ctrl-alt-y) is the readline function
Without specifying
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
输入下面命令
Now at the next prompt, type
a following space), then
Press Alt-Ctrl-y and you'll now see:
现在在下个提示符下,输入echo(跟随一个空格),接着按alt-ctrl-y然后你可以看到:
without pressing Enter yet, do the following不回车,接着做:Press Alt-3 Alt-Ctrl-yPress Alt-- 2 Alt-Ctrl-yNow you will see:现在你看到:
By the way, you could have put the
the line by selecting argument 0:
顺便说,你可以把像引用参数0一样输入echo:
Press Alt-0 Alt-Ctrl-y
To answer the question you added to your original:
现在回答你的补充问题:
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提示符下输入宏:
To make it persistent, add this line to your
如果要永久化,则把下面的这行加入你的~/.inputrc文件:
Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers
不幸的是,这似乎对参数0或负参数不起作用。
原文地址: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-yPress Alt-- 2 Alt-Ctrl-yNow you will see:现在你看到:
$ echo ace
By the way, you could have put the
echoon
the line by selecting argument 0:
顺便说,你可以把像引用参数0一样输入echo:
Press Alt-0 Alt-Ctrl-y
To answer the question you added to your original:
现在回答你的补充问题:
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
~/.inputrcfile:
如果要永久化,则把下面的这行加入你的~/.inputrc文件:
"\eY": "\e2\e."
Unfortunately, this doesn't seem to work for arg 0 or negative argument numbers
不幸的是,这似乎对参数0或负参数不起作用。
相关文章推荐
- 如何在MySQL的存储过程中实现把过程参数用在游标定义的SELECT命令里面作为表名引用
- 如何防止丢失任何 bash 历史命令?
- .NET中如何执行命令行命令?带参数
- bash中的历史命令的参数
- 如何让 vim 可以在命令行执行命令并且附加参数
- oracle安装后完善2-2 如何像Linux 命令行一样可以上下键翻历史命令
- Bash Shell 命令行 选项/参数处理
- 奥巴马访华引用的古诗文如何翻译?
- 如何在FTP命令行执行AS/400命令
- 未引用参数,添加任务栏命令及其它...
- bash if 命令参数说明
- vi 中如何使用 :s 命令实现字串的替换以及对参数分隔符解释
- 如何调试命令行程序?——怎样输入命令行程序参数
- .NET中如何执行命令行命令?
- 如何理解数据结构中的引用参数 &
- 如何删除数据库的日志 参数命令
- 如何在命令行下运行PHP脚本[带参数]
- .NET中如何执行命令行命令?http://dotnet.aspx.cc/article/ebe38f5b-bd7e-42c2-73e4-49616f98be3d/read.aspx
- 如何在windows2000命令行中使用tab命令补全
- .NET中如何执行命令行命令?