If I have an executable - say ~/bin/stuff in my PATH, I can define this in my .zshrc to have it autocomplete text files first as arguments to my command:

zstyle ':completion:*:*:stuff:*' file-patterns '*.txt:all-files' '*:all-files'

However, if stuff is an alias - e.g alias stuff=less, then this doesn't work anymore - and zsh just pretends like it should autocomplete to all-files as that's the default.

I've looked at the docs here - Zsh Completion System and not found anything more than talk of how to expand an alias - which is not what I'm looking for.

I think it's because the docs say argument pattern for zstyle is ':completion:*:*:-command-:*:commands', but does that mean that the fourth section must be a command and not an alias. I always assumed they were more or less interchangeable in zsh.

I mean I can create a bunch one line executables that just have my alias followed by "$@" in them - but that's just not elegant.

  • I have found a workaround where I define the pattern as simply ':completion:*' so it suggests text files first for all commands. This is not ideal, but suffices for now. I would still like this question answered though. – Vidur Aug 1 '18 at 12:36

Assuming tab is mashed after stuff in both these cases

% ls
bar     foo.txt
% whence stuff
% alias stuff=less
% zstyle ':completion:*:*:stuff:*' file-patterns '*.txt:all-files'
% stuff
bar      foo.txt

a relevant knob from zshoptions(1) is

    Prevents aliases on the command line from being internally sub-
    stituted before completion is attempted. The effect is to make
    the alias a distinct command for completion purposes.


% stuff foo.txt

as otherwise the alias is already something else.

To check for side effects we should confirm that this change does not break completion on aliases for which there is no completion or style configuration,

% whence foo
% unfunction _foo
unfunction: no such hash table element: _foo
% alias foo=ls
% foo (eval):1: command not found: _foo

so with COMPLETE_ALIASES set you may need to setup completions for each alias, or list or otherwise include the alias name(s) on the appropriate compdef e.g.

% compdef _ls foo
% foo

On the other hand I do find exec wrappers to be quite elegant, as they unlike shell aliases can be called by exec(3) or system(3) without the need to load up a rather large shell or pollute what should be a simple system call with interactive shell configuration.

  • Interesting. I will try this today and report back. – Vidur Aug 6 '18 at 12:02
  • Yup! This works. Doesn't seem to have any side effects that I have seen yet. – Vidur Aug 7 '18 at 8:23

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.