Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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.

share|improve this answer
You are right, I was shortsighted and I do use VIM to open files in subdirectories. The Superuser link helped. Thanks! – dotancohen Nov 29 '12 at 12:51

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.

share|improve this answer
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 '12 at 12:52

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...

share|improve this answer
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 '12 at 12:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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