I would like that bash completes my command vim myPrTab to vim myProgramName.c even if the current folder also contains a file called myProgramName.o, since 99.9% of the time I want to open the source with vim not the executable.

When executing this works very well: ./myPTab gets completed to ./myProgramName.o. I would prefer if this stayed the way it is.

How can I achieve to automatically complete to *.c except when executing with ./…?

  • 1
    In ZSH, this is done with something like zstyle ':completion:*:*:(diff|*grep|less|vi|vim):*:all-files' ignored-patterns '*.core' '*.o' '*.midi' to ignore certain patterns from text-reading programs. – thrig Mar 15 '16 at 16:44
  • @thrig I'm using bash at the moment, do you know any way how to achieve a similar effect in bash? – LastSecondsToLive Mar 15 '16 at 17:07

This stuff is defined in your bash_completion script. Sometimes you can find that in /etc and sometimes in /usr/share/bash-completion.

Mine has this line:

_install_xspec '*.@([ao]|so|so.!(conf|*/*)|[rs]pm|gif|jp?(e)g|mp3|mp?(e)g|avi|asf|ogg|class)' vi vim gvim rvim view rview rgvim rgview gview emacs xemacs sxemacs kate kwrite

It does exactly what you want.

I imagine your installation is too old. You can try updating "bash-completion", or editing that file yourself.

Alternatively, you could have your .bashrc edit the _xspecs variable manually -- bash-completion does not provide an interface to do this for you.

Edit: it turns out the OP was not using the bash-completion package at all (so just using the built-in "dumb" filename completion). In that case installing it and following the setup instructions is sufficient. On Linux this package is typically installed by default, and no user setup is required.

  • Okay, I since I'm on a mac I downloaded bash-completion via brew. Where do I need to put the script now? Do I need to create a specific file in /usr/share/bash-completion/? And how does it get sourced? – LastSecondsToLive Mar 15 '16 at 23:01
  • If you didn't have bash-completion before then you probably don't need to modify it at all. You should source it in your .bashrc file, but you might find the system bashrc already did. – ams Mar 15 '16 at 23:07
  • Ok, I created a file completion.sh in /usr/local/etc/bash_completion.d. That's where the installation description told me to put scripts. This file now contains your line, but I get -bash: _install_xspec: command not found on start-up. Any idea? – LastSecondsToLive Mar 15 '16 at 23:09
  • Don't bother. If you have a new version of bash-completion the rule is already there. – ams Mar 15 '16 at 23:15
  • You just need to make sure that bash-completion is enabled in your shell. – ams Mar 15 '16 at 23:16

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.