55

I had this on my Ubuntu setup and since I switched to Fedora I want to set it and I forgot how... The idea is simple :

I don't want the terminal to show me suggestions when I double tab, instead I want it to cycle through every possible suggestion with each press on tab... this can be done in Vim also.

So when I type gedit a and press tab it will show me every file with a first letter a.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
vanjadjurdjevic
  • 797
  • 1
  • 10
  • 13

2 Answers2

65

This is actually a readline feature called menu-complete . You can bind it to tab (replacing the default complete) by running:

bind TAB:menu-complete

You probably want to add that to your ~/.bashrc. Alternatively, you could configure it for all readline completions (not just bash) in ~/.inputrc.

You may also find bind -p (show current bindings, note that shows tab as "\C-i") and bind -l (list all functions that can be bound) useful, as well as the bash manual's line editing section and readline's documentation.

derobert
  • 107,579
  • 20
  • 231
  • 279
  • Can i combine 2 methods of autocompleting (the list and inline) and thanks for the answer :) – vanjadjurdjevic Nov 12 '11 at 00:44
  • 3
    @vanjadjurdjevic: Sure, just bind them to different keys. – derobert Nov 12 '11 at 09:51
  • 6
    `menu-complete` is cool, but it hides the list of all possible suggestions ;-( Is it possible to both see the list and cycle through options? – Ciro Santilli OurBigBook.com Mar 06 '16 at 10:39
  • 3
    @CiroSantilli六四事件法轮功包卓轩 I also like this feature in readline based shells. Currently the zsh way to do it is very cool: first hit of tab shows all possibilities, second hit of tab starts cycling the completion items. – xuhdev Mar 19 '16 at 22:44
  • @xuhdev Yeah, we shouldn't expect Bash to get those feature done anytime soon... but I'm too lazy too switch! Nice to hear from you xudev, fellow productivity addict :-) – Ciro Santilli OurBigBook.com Mar 19 '16 at 22:52
  • 1
    Let's pretend you are using `cd` and `menu-complete` and cycling over directories. What key do you press to "pick" that directory and start cycling the contents of that directory? Is there another thing to set/bind? – Tony Jul 29 '16 at 21:31
  • @Tony forward slash works pretty well. Really any key does, as it resets where completion starts from. Not sure if there is another keybinding as well... Seems like that might make a good question here. – derobert Jul 29 '16 at 21:33
  • @derobert but the thing is, if I type forward slash (or another key), it will input that into my terminal and i have to delete it to continue cycling in that directory? Does that make sense? – Tony Aug 01 '16 at 15:10
  • 1
    @Tony yeah, it'll insert it. But if you use forward slash, that's OK—a path like "`/usr/local//`" is still perfectly valid, and will start menu-completing things inside `/usr/local`. – derobert Aug 09 '16 at 18:18
  • 1
    @CiroSantilli新疆改造中心六四事件法轮功 I've added an answer that will give you this feature! :) https://unix.stackexchange.com/a/447638/191530 – gmarmstrong Jun 03 '18 at 18:33
13

You can cycle through the completion menu in Bash, and you can also show the menu of items. Unlike in Zsh, the current menu item will not be highlighted.

Add to ~/.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

Documentation from 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.
gmarmstrong
  • 1,183
  • 1
  • 15
  • 35
  • Thanks, this is really useful when I have Unicode characters in the filename that I don't want to take the trouble to type. – Yan King Yin Apr 04 '20 at 14:09