When using Tab for completion on the Bash shell prompt, is there any way to configure it to complete only files for the command vim and only directories for the command cd? In large directories with many files and subdirectories, the should reduce by half the amount of corrections to be done while completing.

Note that moving to a different shell is impractical, but I would still be interested in hearing non-Bash solutions if no Bash solution exists.

3 Answers 3


As mentioned installing (and possibly enabling) bash-completion is the easiest (or at least most functional) method...

These command will however also do that: (Put it in ~/.bashrc and/or ~/.bash_profile or somewhere to run it every time you open a console)

complete -d cd
complete -f vim

See bash's man page and help complete for more info / options.

  • Thank you. Though this is quite the answer to the question, in fact as Gertvdijk pointed out I was shortsighted in my thinking.
    – dotancohen
    Nov 29, 2012 at 12:52

is there any way to configure it to complete only files for the command vim?

There may be, but be careful what you wish for. It would make it impossible to have tab-completion for files in subdirectories as well as it won't expand for directories with files as well.

and [ is there any way to configure it to complete ] only directories for the command cd?

Yes, that behaviour is installed by default in Ubuntu 12.04 and there may be other OSs too. If you're not that lucky, try this answer on Superuser.com.

  • 1
    You are right, I was shortsighted and I do use VIM to open files in subdirectories. The Superuser link helped. Thanks!
    – dotancohen
    Nov 29, 2012 at 12:51

Have you got the bash-completion package installed?

Autocompletion with vim will also select directories, as directories almost always have files in them. You can also open a directory with vim and then traverse the directory tree from within vim.

On my system, bash autocompletion for cd does only list directories, so it is possible, and is what happens by default on all of my systems...

  • This is on Ubuntu Server 12.10. It does seem to have 'bash-completion' installed by default, but cd completes file names anyway.
    – dotancohen
    Nov 29, 2012 at 12:50

You must log in to answer this question.

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