0

I am using zsh.

I have a command foo, and I use alias foo="sudo foo" as a shortcut. I want zsh to complete the command just like I typed foo.

I googled and found compdef _foo f=foo which can deal with it. When I type f something<tab>, the completion works fine. But when I try compdef _foo foo=foo and then foo something<tab>, it does not work.

Is there a way to deal with the Tab completion when I use an alias with the same name as the original command?

kqwyf
  • 84
  • 7
  • 1
    look at `setopt complete_aliases` and try to set it or unset it depending on your current state. I also have aliases masking original commands and autocomplete for them is working fine. – rcwnd_cz Aug 24 '20 at 08:50
  • You are over complicating. You do not need 'compdef'. – Ahmad Ismail Aug 25 '20 at 23:12
  • Thanks for your kind help! I found it a mistake of mine that made things worse. I will explain it in the answer. – kqwyf Aug 30 '20 at 08:49

1 Answers1

1

The problem is actually a special case happened to me.

As @blueray said, zsh is able to complete commands like sudo foo or an alias of it normally.

Thanks to @rcwnd_cz , I found that adding setopt complete_aliases solves my problem. The point is that I set an alias somewhere else that maps sudo to sudo FOO=bar. As a result, alias foo="sudo foo" is an "alias in alias", and zsh refuses to complete it. Setting setopt complete_aliases solves it perfectly.

kqwyf
  • 84
  • 7