42

Is it possible to configure bash in such a way that on the first tab autocomplete it lists all possible files and on subsequent ones cycles through the choices?

Both options are easy to do separately and I could bind them to different keys, but the above would be perfect, but I can't find anything about it on the net.

Voo
  • 815
  • 1
  • 8
  • 14
  • 3
    It's been a while since I have used ZSH, but I believe its autocomplete functions like this. – jordanm Nov 08 '12 at 18:07
  • 1
    @jordanm Well seems like an option if nothing else works, but that would also entail updating everything from .bashrc, etc. to the ZSH equivalents, which I'd quite like to avoid. – Voo Nov 08 '12 at 18:34

3 Answers3

53

This seems close to what you want:

bind "TAB:menu-complete"
bind "set show-all-if-ambiguous on"
Jim Paris
  • 14,137
  • 5
  • 36
  • 35
  • 1
    Almost perfect, apart from the small flaw that it also autocompletes on the first tab, which can be a bit annoying if I'm just trying to figure out the exact name (i.e. I have to delete quite a bit more characters if I want to change then), but I'm quite willing to live with that. – Voo Nov 14 '12 at 05:38
  • 1
    How can this easily be undone? – dingalapadum Oct 13 '15 at 08:53
  • 1
    `bind "TAB:complete"; bind "set show-all-if-ambiguous off"` – Jim Paris Oct 14 '15 at 13:54
  • 20
    @Voo add the third line `bind "set menu-complete-display-prefix on"` will just list the candidates on the first tab without auto-completion. – Naitree Nov 17 '15 at 13:00
  • 3
    @Naitree can't upvote your suggestion enough. It works and IS so, so much better than the default bash behavior. Thank you. – JBeurer Dec 06 '16 at 21:34
  • It might be worth noting that both `→` and `ESC` work for going into the current folder and continuing cycling there. – Felix Dombek Jul 25 '19 at 13:43
20

This is what I use. As far as I can tell it does exactly what you want.

# make tab cycle through commands after listing
bind '"\t":menu-complete'
bind "set show-all-if-ambiguous on"
bind "set completion-ignore-case on"
bind "set menu-complete-display-prefix on"

This works on Mac (10.13 & 10.14) and Ubuntu (16.04 & 18.04).

Milo
  • 301
  • 2
  • 2
1

Have you considered? It was referenced at Superuser ...

[[ $- = *i* ]] && bind TAB:menu-complete
tink
  • 6,160
  • 2
  • 21
  • 30
  • 1
    `menu-complete` is only half of what the question is asking. As the question says, *"[b]oth options are easy to do separately"*. – jw013 Nov 08 '12 at 19:18
  • Yes I know how to change the behavior to one of the two options, but that's the easy part. The real problem is how to combine the two into one. – Voo Nov 10 '12 at 04:46
  • Can you explain to me the detail of what's going on in that line? – Tom Feb 15 '20 at 01:49