终端自动完成:循环浏览建议


37

我在Ubuntu安装程序上安装了此软件,并且由于我切换到Fedora,所以我想对其进行设置,却忘记了如何做……这个想法很简单:

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

因此,当我键入gedit a并按tab它时,它将向我显示每个文件的第一个字母a


Answers:


51

这实际上是一个称为的readline功能menu-complete。您可以complete通过运行以下命令将其绑定到tab(替换默认值):

bind TAB:menu-complete

您可能想将其添加到您的中~/.bashrc。或者,您可以在中为所有readline补全(不仅仅是bash)配置它~/.inputrc

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


3
@vanjadjurdjevic:可以,只需将它们绑定到不同的密钥即可。
德罗伯特

4
menu-complete很酷,但是它隐藏了所有可能建议的列表;-(是否有可能同时看到列表和在选项之间循环?
Ciro Santilli新疆改造中心法轮功六四事件

2
@CiroSantilli六四事件法轮功包卓轩我也喜欢基于readline的shell中的此功能。当前,zsh的实现方法非常酷:选项卡的第一次点击显示所有可能性,选项卡的第二次点击开始循环完成项目。
xuhdev '16

1
让我们假设你正在使用cdmenu-complete和骑自行车过目录。您按什么键“选择”该目录并开始循环该目录的内容?还有另一件事要设置/绑定吗?
托尼

1
@Tony是的,它将插入它。但是,如果使用正斜杠,那就可以了-像“ /usr/local//” 这样的路径仍然是完全有效的,并且会在菜单中启动菜单完成功能/usr/local
derobert

6

您可以循环浏览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

来自的文档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.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.