58

I'm using bash, but I'd like to have zsh style autocomplete (you hit tab and it tabs through the possibilities), rather than what bash seems to do, which is display a list of possibilities but not choose anything until I type some more to disambiguate. How can I get zsh type behavior in bash?

Searching for a solution has turned up lots of answers to other questions, so I'm hoping I can get a simple answer here (i.e. what to paste into my .bashrc).

(And to answer the obvious question, I need to use bash here because I just joined a team and they do some stuff to set up bash to make the environment easier to work in. I can probably eventually make sure I have it working the same way in zsh, but for now it's easier if I use bash and just get it behaving more like zsh during interactive use.)

4 Answers 4

39

I use

bind 'TAB:menu-complete'

to achieve it

3
  • 16
    When I add this to my .bashrc, tab cycles through the possible options rather than listing them in the terminal. Is is possible to get both behaviors? Aug 17, 2014 at 13:08
  • 1
    For Shitf-Tab to complete backwards, use bind '"\e[Z":menu-complete-backward'.
    – Ainar-G
    Nov 15, 2019 at 10:54
  • Add bind 'set show-all-if-ambiguous on' to list available options in addition to cycling through with tab.
    – caylus
    Dec 19, 2022 at 11:41
47

To get first completion and a listing you can add the following to bashrc

bind 'set show-all-if-ambiguous on'
bind 'TAB:menu-complete'

show-all-if-ambiguous: 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. The default value is ‘off’.

see Bash Manual for more information.

Edit:

This doesn't make bash work exactly as zsh tho. Zsh will complete up until the next ambiguous match. Bash will just cycle through all matches.

e.g.

$ ls ~/.ba<tab> .bashrc .bash_history .bash_profile

  • zsh: will complete up until ~/.bash and present a list of matches which handily enables you to append _ and hit <tab> again.
  • bash: will just cycle through all ~/.ba* matches.
4
  • 13
    I liked using the arrow keys, and the visual indication of zsh's autocompletion. Is there a way to get those behaviors with bash? I switching back for better server compatibility
    – CESCO
    Feb 3, 2016 at 22:52
  • 1
    Why do you use the bind command before set? Isn't it enough to just set the setting to on? Are there any advantages?
    – winklerrr
    Feb 19, 2020 at 8:18
  • I actually like the bash behavior better. Thanks for explaining this and giving the excellent answer!
    – mcp
    Aug 26, 2020 at 17:30
  • @winklerrr we're setting a built-in variable of the readline library, for which you the bind command. You can list all the readline variables with bind -v. What you're referring to is set which sets a bash variable.
    – ingkebil
    Aug 27, 2020 at 19:06
21

In Bash: Zsh like autocomplete can be achieved with Ble.sh.

# Quick TRIAL without installation
# requires the commands git, make (GNU make), and gawk

git clone --recursive https://github.com/akinomyoga/ble.sh.git
make -C ble.sh
source ble.sh/out/ble.sh

Here is a quick demo of Bash with blesh. For more details see: README of Ble.sh

enter image description here

4
  • 4
    May I know the reason why it's unnecessarily downvoted without any explanation? Please help to improve this answer by suggesting an edit instead of downvoting without any insightful and actionable details.
    – Rakib Fiha
    Apr 28, 2021 at 7:56
  • 1
    That's exactly what i was searching for! Thanks.
    – mathway
    Feb 8, 2022 at 22:20
  • 1
    This is the best answer. Dependencies for ble.sh are gmake & gawk. Prefer gmake -C ble.sh, if possible. To install under ~/.local/share/blesh use gmake install (or make install) and then add source /home/ipellegrini/.local/share/blesh/ble.sh to your .bashrc (or .bash_profile on MacOS). Jan 12, 2023 at 20:34
  • 1
    Love this answer. Works as expected. Jun 28, 2023 at 9:24
2

Following up on ingkebil's answer, for those that put bind and set into inputrc:

$ tail -n 4 /etc/inputrc
# zsh like completion:
# https://superuser.com/questions/288714/bash-autocomplete-like-zsh
set show-all-if-ambiguous on
TAB:menu-complete

I like keeping my .bashrc clean.

1
  • 10
    ... and instead pollute the system-wide inputrc? Fine if it's your personal system; dubious as general advice.
    – tripleee
    Jan 30, 2022 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .