是否可以通过选项卡浏览特定命令的命令行历史记录?

是否可以通过选项卡浏览特定命令的命令行历史记录?

例如,我可以看到我之前传递给命令 ssh 的所有参数(即我在调用 ssh 时作为参数传递的所有服务器地址)吗?

这将类似于“历史”命令,但不完全一样。

如果我可以浏览这个命令历史记录,那就太棒了,但这可能要求太多了。例如,如果我输入 ssh,然后按 Tab 键将允许我循环浏览之前执行的命令。

答案1

不完全是你要求的,但你可以搜索历史记录。

例如,^R随后使用ssh,然后继续循环执行命令^R

(这是反向搜索。正向搜索默认为 ,^S但不幸的是,它与典型终端的 XOFF (用 撤消^Q)冲突,因此您可能需要重新映射它以使其有用。)

答案2

这个有可能。您想要研究 Ctrl-R 和一些历史扩展。

man bash

reverse-search-history (C-r)
    Search backward starting at the current  line  and  moving 'up'
    through  the  history  as  necessary.   This  is  an incremental
    search.

History expansions introduce words from the history list into the input
stream,  making  it  easy to repeat commands, insert the arguments to a
previous command into the current input line, or fix errors in previous
commands quickly.

关于反向搜索历史的进一步解释:

按 Ctrl-R 并开始输入字符串。您将看到历史记录中的最后一行,其中包含任意位置的字符串,并为每次按键更新。多按 Ctrl-R 即可查看前面的行,而无需输入新字符。按 Enter 接受并启动命令,左右编辑命令,上下浏览历史记录,不限制从您找到的行开始搜索的字符串,Ctrl-C 放弃搜索。

答案3

这也很有用:

history-search-backward
      Search backward through the history for the string of characters between the start
      of the current line and the point.  This is a non-incremental search.

默认情况下可能不会绑定,但您可以将它和 hs-forward 绑定到例如向上翻页/向下翻页。编写命令行的开头并点击 pgup/pgdown 然后可以滚动浏览从该命令行开始的命令行。我更进一步,将它们也绑定到向上/向下箭头键。

/etc/inputrc$HOME/.inputrc

# pgup, pgdown, up, down
"\e[5~" history-search-backward
"\e[6~" history-search-forward
"\e[A" history-search-backward
"\e[B" history-search-forward

相关内容