如何使bash历史前缀敏感?

rip*_*234 8 bash command-history

如何在 bash 上按上/下键调出以我已经输入的前缀开头的最后一个命令?

例如

$ ls foo
$ echo hello
$ ls <UP_ARROW>
Run Code Online (Sandbox Code Playgroud)

输入 ls 后单击 UP 应显示“ls foo”而不是“echo hello”

And*_*ndy 11

几种可能:

  1. 绑定history-search-backward/forward命令。我相信这最接近你想要的:

    bind '"\e[A":history-search-backward'
    bind '"\e[B":history-search-forward'
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用历史搜索。这大概是必然的^R。在一个空行上,^Rls<space>会让你回到ls foo,然后^R再次带你到最后一个ls命令(或其他命令与行中ls某处)ls foo,等等......

  3. 绑定magic-space命令:bind SPACE:magic-space. 然后键入(在空行上)!ls<space>,readline 将自动执行历史替换,为您留下ls foo.