10

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.

2 Answers 2

10

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.

5
  • Cool. Any way to do this with a whitelist instead of a blacklist?
    – lindhe
    Mar 7, 2017 at 9:34
  • @lindhe The ^ at the beginning of the pattern means “all except what follows”. Mar 7, 2017 at 12:14
  • Oh, I thought it was the "start of line" character. Nice. And that would work similarly -- prioritizing .tex, but giving the others if there is no tex available?
    – lindhe
    Mar 7, 2017 at 12:24
  • I tried it, and with only *.(tex) as argument, it does not do any suggestions form me. With *.tex it only suggests .tex files. I want it to prioritize tex, not completely ignore the others. :/
    – lindhe
    Mar 7, 2017 at 12:29
  • OK, no, sorry. You are right. It works as intended. :) Thanks.
    – lindhe
    Mar 7, 2017 at 12:35
7

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.

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.