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

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

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

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

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

答案1

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

bind TAB:menu-complete

您可能想将其添加到您的~/.bashrc.或者,您可以将其配置为所有 readline 完成(不仅仅是 bash)~/.inputrc

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

答案2

您可以在 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.

答案3

通过新的 shell 环境的替代解决方案:

我建议将终端环境从 bash 更改为默认情况下,它具有自动完成功能以及语法突出显示和其他一些功能。

在此输入图像描述

相关内容