如何根据之前输入的命令使终端自动完成?

mak*_*i57 5 command-line bash bashrc bash-history

使用终端时,按上下循环按时间顺序输入以前的命令是正常的。

我从一个站点学到了如何通过在向上和向下按下时根据先前输入的命令自动完成我在光标处键入的内容来添加该行为。

例如,假设我按此顺序键入以下命令:

  1. ls Videos
  2. ls Vids
  3. lsomething
  4. ls Videos/Funny/Old
  5. vi file
  6. ls Music

如果我没有输入任何内容,按上下键会像往常一样循环显示所有行。但是,如果我输入ls了 ,则上下按会循环显示前 2 个字符所在的所有命令ls(上面的第1、2、3、4、6 行)。但是输入ls(后面有一个空格)只会让上下循环通过第 1、2、4、6 行。类似地,ls Vid让箭头在第 1、2、4ls Videos行循环,但只允许在第 1 行和第 4 行之间循环。

完成特定于您需要一遍又一遍输入的命令的值非常有用,而不必循环浏览您在它们之间输入的每个不相关的命令。例如,我试图测试env诸如语言环境之类的值对事物的影响:

env [many different values] [command]
[insert many different commands here]
env [slightly different values from before] [command]
[doing other stuff]
Run Code Online (Sandbox Code Playgroud)

我只需要在键入后按几次即可在env我尝试过的值之间循环。

如果描述的方式有点长,我深表歉意。我不知道它叫什么。我的终端曾经有这种行为,但他们似乎已经失去了它,我找不到我从中学到的网站。我认为这与编辑有关~/.bashrc

ter*_*don 5

要获得您想要的行为,请将这些行添加到您的文件中~/.inputrc(如果文件不存在,则创建该文件):

"\e[A": history-search-backward
"\e[B": history-search-forward
Run Code Online (Sandbox Code Playgroud)

然后,打开一个新终端,它将按预期工作。/etc/inputrc此行为由和中的设置控制~/.inputrc。您可以在Readline Command Names以下部分阅读有关它们的更多信息man bash

   non-incremental-forward-search-history (M-n)
          Search forward  through  the  history  using  a  non-incremental
          search for a string supplied by the user.
   history-search-forward
          Search  forward through the history for the string of characters
          between the start of the current line and the point.  This is  a
          non-incremental search.
Run Code Online (Sandbox Code Playgroud)