17

When I am in a directory in bash, and I press cd Space Tab, it shows everything in the directory as a possibility. (Show all 1000 possibilities?) This is really cumbersome when I am in a directory with lots of regular files and relatively few directories.

So, is it possible to make the choices for autocompletion of cd to only include directories?

I know I can get a directory listing within a directory by doing

ls -d */

but I'm not sure how to proceed from there.

I am using CentOS 6.6 Final.

2 Answers 2

32

Just add

complete -d cd

in your ~/.bashrc (or other bash configuration file).

8
  • it works! thanks. I had to type bash after to reload my ~/.bashrc file
    – chiliNUT
    Feb 23, 2015 at 19:37
  • That's interesting. This is the default behavior on my Debian install, but typing complete doesn't show -d cd or anything like it. Apparently it's been done in some other way here.
    – Tom Zych
    Feb 23, 2015 at 19:40
  • @TomZych There is popular bash completion script, it is very likely that your distro installed it by default.
    – jimmij
    Feb 23, 2015 at 19:44
  • 2
    To re-read ~/.bashrc type . ~/.bashrc. This may it does not start a sub-shell. Feb 23, 2015 at 23:01
  • 1
    @richard One can also just re-type/paste this command in current interactive shell.
    – jimmij
    Feb 23, 2015 at 23:13
5

This should be happening automatically on a typical install on many distros.
If it is not, you're probably missing the bash-completion package:

  • Ubuntu: sudo apt-get install bash-completion
  • Arch: sudo pacman -S bash-completion
1
  • On Debian Bullseye 11.2, this is the default for all user EXEPT root. In order to have the same behaviour on user root, you have to manually edit your bashrc file to source the bash_completion package.
    – 4wk_
    Apr 4 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.