Linux 在 reverse-i-search (Ctrl+R ) 中,在历史记录中的相似命令之间切换的任何方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14231643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In reverse-i-search (Ctrl+R ) ,Any method to switch between similar commands in history
提问by Arun
I think an example will be good to understand my question.
我认为一个例子会很好地理解我的问题。
...
scp file1 [email protected]:/home/user1/linuxfiles/samplecode/important
...
...
scp file1 [email protected]:/home/user1/linuxfiles/samplecode/important/tested
...
...
Assume that is the order of commands in history. If I am doing Ctrl+Rand type scp
it will show the last executed scp
command ( ie the line ending with 'tested') .
假设这是历史命令的顺序。如果我正在执行Ctrl+R并键入scp
,它将显示最后执行的scp
命令(即以“tested”结尾的行)。
But I want to locate the scp command ending with 'important'. So is there any way in this reverse-i-search
to view all commands starting with scp, to choose the appropriate one?
但我想找到以“important”结尾的 scp 命令。那么有什么方法reverse-i-search
可以查看所有以 scp 开头的命令,选择合适的命令吗?
采纳答案by jgr
Keep pressing Ctrl-Rand it will traverse your history.
继续按Ctrl-R它会遍历你的历史。
回答by Bonlenfum
If your search terms are a bit more complicated/ not contiguous, another option is to grep among the history
results, e.g.:
如果您的搜索词有点复杂/不连续,另一种选择是在history
结果中使用grep ,例如:
history 300 | grep scp | grep important$
This will return a list of commands in your history that match, such as:
这将返回历史记录中匹配的命令列表,例如:
3323 scp file1 [email protected]:/home/user1/linuxfiles/samplecode/important
3325 scp file1 [email protected]:/home/user1/winfiles/samplecode/important
And you can then execute the relevant command with !3325
.
然后您可以使用!3325
.
I sometimes find this useful when running a lot of similar commands and may have to press Ctrl+Rmany times to get back to the exact command.
我有时会发现这在运行许多类似的命令时很有用,并且可能必须多次按Ctrl+R才能返回到确切的命令。
回答by GiorgosK
There is great alternative to Ctrl+R
有很好的替代品Ctrl+R
install https://github.com/dvorka/hstr
安装https://github.com/dvorka/hstr
run it with hh
运行它 hh
it shows a list of you all the last commands executed (contents of ./bash_history)
它显示了您最后执行的所有命令的列表(./bash_history 的内容)
if you start typing the list will be filtered based on what you type
如果您开始输入,列表将根据您输入的内容进行过滤
you can use Up/Downarrows to select the desired command and select it with Enter
您可以使用Up/Down箭头选择所需的命令并使用Enter