12

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.

24

Just add

complete -d cd

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

  • it works! thanks. I had to type bash after to reload my ~/.bashrc file – chiliNUT Feb 23 '15 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 '15 at 19:40
  • @TomZych There is popular bash completion script, it is very likely that your distro installed it by default. – jimmij Feb 23 '15 at 19:44
  • 2
    To re-read ~/.bashrc type . ~/.bashrc. This may it does not start a sub-shell. – ctrl-alt-delor Feb 23 '15 at 23:01
  • 1
    @richard One can also just re-type/paste this command in current interactive shell. – jimmij Feb 23 '15 at 23:13
2

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

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.