Bash Zsh Autocomplete – Ignore Certain Files in Directory with Autocomplete

autocompletebashzsh

What I'm asking is a little bit specific, and might be a different than other autocomplete questions on Unix Stackexchange.

Suppose I have a directory that looks like this

-rw-r--r-- 1 hlin117 staff 1.1K Sep 19 13:05 doc.aux
-rw-r--r-- 1 hlin117 staff  26K Sep 19 13:05 doc.log
-rw-r--r-- 1 hlin117 staff 177K Sep 19 13:05 doc.pdf
-rw-r--r-- 1 hlin117 staff  13K Sep 19 13:01 doc.tex

It makes very little sense to try doing vim doc.pdf, and in the common case, I wouldn't be doing vim doc.log or vim doc.aux. Instead, I'd often do

vim doc.tex

Unfortunately, tab-autocomplete will suggest to me all 4 files instead of only doc.tex.

Is there a way where I could type vim \t, and this would ignore some certain files in my directory?

More generally, can I type command X \t, and write some setting where typing command X will ignore files in my directory?

FYI: I use zsh. Not sure whether bash and zsh will have similar solutions.

Best Answer

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.