终端自动完成:循环显示建议

van*_*vic 48 bash autocomplete

我在我的 Ubuntu 设置中有这个,自从我切换到 Fedora 我想设置它但我忘记了如何......这个想法很简单:

我不希望终端在我 double 时向我显示建议tab,而是希望它在每次按下时循环显示所有可能的建议tab......这也可以在 Vim 中完成。

所以当我输入gedit a并按下tab它时,它会显示每个文件的第一个字母a

der*_*ert 62

这实际上是一个名为menu-complete. 您可以complete通过运行将其绑定到选项卡(替换默认值):

bind TAB:menu-complete
Run Code Online (Sandbox Code Playgroud)

您可能想将其添加到您的~/.bashrc. 或者,您可以在~/.inputrc.

您可能还会发现bind -p(显示当前绑定,注意将选项卡显示为"\C-i")和bind -l(列出所有可以绑定的函数)以及bash 手册的行编辑部分readline 的文档很有用。

  • `menu-complete` 很酷,但它隐藏了所有可能建议的列表;-( 是否可以同时查看列表和循环选项? (6认同)
  • @vanjadjurdjevic:当然,只需将它们绑定到不同的键即可。 (3认同)
  • @CiroSantilli六四事件法轮功包卓轩我也喜欢基于readline的shell中的这个功能。目前 zsh 的方式非常酷:第一次点击选项卡显示所有可能性,第二次点击选项卡开始循环完成项目。 (3认同)

gma*_*ong 11

您可以在 Bash 中循环浏览完成菜单,也可以显示项目菜单。与 Zsh 不同,当前菜单项不会高亮显示。

添加到~/.inputrc

set show-all-if-ambiguous on
set show-all-if-unmodified on
set menu-complete-display-prefix on
"\t": menu-complete
"\e[Z": menu-complete-backward
Run Code Online (Sandbox Code Playgroud)

文档来自man bash

Readline Variables
    menu-complete-display-prefix (Off)
           If set to On, menu completion displays the common prefix of the
           list of possible completions (which may be empty) before cycling
           through the list.
    show-all-if-ambiguous (Off)
           This alters the default behavior of the completion functions. If
           set to On, words which have more than one possible completion
           cause the matches to be listed immediately instead of ringing
           the bell.
    show-all-if-unmodified (Off)
           This alters the default behavior of the completion functions in
           a fashion similar to show-all-if-ambiguous. If set to On, words
           which have more than one possible completion without any
           possible partial completion (the possible completions don't
           share a common prefix) cause the matches to be listed
           immediately instead of ringing the bell.

Completing
    menu-complete
          Similar to complete, but replaces the word to be completed with
          a single match from the list of possible completions. Repeated
          execution of menu-complete steps through the list of possible
          completions, inserting each match in turn. At the end of the list
          of completions, the bell is rung (subject to the setting of
          bell-style) and the original text is restored. An argument of
          n moves n positions forward in the list of matches; a negative
          argument may be used to move backward through the list. This
          command is intended to be bound to TAB, but is unbound by
          default.
    menu-complete-backward
          Identical to menu-complete, but moves backward through the list
          of possible completions, as if menu-complete had been given
          a negative argument. This command is unbound by default.
Run Code Online (Sandbox Code Playgroud)