Bash/zsh tab autocomplete: Given initial command, ignore certain files in directory with autocomplete

In zsh, with the “new” completion system (i.e. if you have compinit in your .zshrc), use the file-patterns style.

zstyle ':completion:*:*:vim:*' file-patterns '^*.(aux|log|pdf):source-files' '*:all-files'

Files matching the pattern *.(aux|log|pdf) will only be completed on the vim command line if there would otherwise be no completion.

You can use a pattern for the command name, in particular * to match all commands except the ones that are matched explicitly.


Adding to this because I don't use zsh, and there is much more general option.

Just place in your .bashrc a line using the bash builtin command complete. For example:

complete -f -X '*.@(aux|log|pdf)' -o plusdirs vim

where -f will complete filenames, -X filters out matching patterns from the possible choices with a glob string, and -o plusdirs will afterward add subdirectories to the list.