Is it possible to make a custom zsh completion file,so when hitting tab for filenames it avoids completing the binaries? For example,I have 2 files , myprogram.c and myprogram,the binary,is it possible so when I do vim myprog(tab) it automagically completes it to myprogram.c,not myprogram? Thanks in advance!
2 Answers
You can ignore some patterns in completions by tuning completion styles with the zstyle
built-in. There are examples in the zsh guide.
For example, to ignore *.o
files when completing files to edit with vim, you can put this in your .zshrc
:
zstyle ':completion:*:*:vim:*:*files' ignored-patterns '*.o'
This won't work to exclude executables in a useful way. You can exclude *~*.*
, or more precisely (|*/)[^/.]##
, but this excludes every file name that doesn't contain a .
, including scripts and even worse directories. As far as I know, the completion ignore mechanism can't check for the existence or type of files, it just matches text patterns.
-
This does not work for me for some reason. If I replace
vim
withv
for example and then complete onv
it does, but not usingvim
. I suspect it's the default complete function in_vim
that is responsible. Any idea how I can enforce what is coming from.zshrc
? Dec 22, 2016 at 11:17 -
@SebastianBlask It works for me with zsh 5.0.5 and a
.zshrc
that contains onlyautoload compinit; compinit
and thezstyle
line in my answer. So the problem is not the_vim
function (unless it's very different in your version), but in some other conflicting setting in your configuration. Dec 22, 2016 at 12:36 -
thanks for checking. I found the problem, I am using scm breeze and vim is aliased so I can do
vim 2
where 2 is the index of anls
. Did more googling and experimenting without much success so in the end I got this to work:zstyle ':completion:*:*:*:*:*files' ignored-patterns '*.pyc'
. Not application specific, but good enough for me at the moment. Dec 22, 2016 at 15:13
You could of course tinker with this so that it worked for any given folder or set of files, but first you should understand the reason it doesn't do that now.
On linux, file extensions don't matter, the only thing that matters is the execute bit. Things can be executed whether they are really binary or just text in the form of a script. Your criteria of "not binaries" does not match up well with the distinctions made by Linux about what a file is or isn't.
That being said it wouldn't be too hard to write a custom completion function to replace the file name glob completion for a given command that would list all files but exclude the ones that have '.c' versions. It would require some overhead that would slow the tab completion down a little.