Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers 3

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

 
discard

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.