You are not logged in.

#1 2016-09-09 02:58:54

ohmysql
Member
Registered: 2015-03-13
Posts: 33

zsh alias tab completion in sudo

I've been googling and unable to find an answer for this. What I'd like to do is have sudo be able to tab complete my aliases in zsh.

For instance:

alias hello='echo hi'
hell[tab] [enter]
  "hi"
sudo hell[tab]

When I set those kinds of aliases in zsh, I'd like to be able to sudo [tab] complete them.

Any idea how to make that happen, or why it's working in bash but not zsh? I just have the default grml zsh installed and only a few aliases in a separate file.

Thanks!

Like,
Oh
my
SQL!!!

Offline

#2 2016-09-09 08:49:22

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: zsh alias tab completion in sudo

I think it works in bash because the tab completion is not as smart as in zsh. Aliases are normally only expanded when they occur at the beginning of the command line. So while bash completes "sudo hello", it executes this command without changing it to "sudo echo hi" and you'll see "sudo: hello: command not found" (unless you have some executable called "hello", but the tab completion doesn't take this into account).

zsh also has global aliases that would make this example work ("hello" as the second word gets expanded to "echo hi", which sudo can execute), but these don't seem to be completed at all and I believe they're not used very often. I found this stackexchange q&a that suggest some abbreviation mechanism as an alternative (slightly different use case, though). A script called "hello" in, for example, /usr/local/bin would be another alternative.

Offline

#3 2016-09-10 22:28:22

ohmysql
Member
Registered: 2015-03-13
Posts: 33

Re: zsh alias tab completion in sudo

Too smart? That is a helpful tip, if not exactly the solution I had in mind. And I'm not sure I can count it as a solution because there is still no tab completion for abbreviations wink I did figure out how to change the abbrevation completion key smile

K, lol anyway, btw pretty sure the abbreviation thing is programmed into grml, not a native zsh feature.

That said, this is a cool feature and I'm grateful you pointed it out. Kind of a shame to need to translate my aliases into abbreviations, but it wasn't too hard. I'll see if I like it.

I'll not mark this as solved in case someone has another solution? I think I'd prefer for my aliases to work under sudo. Also, moving the aliases file to the root folder didn't solve this in sudo for me. I think it's just about environment variables.

Thanks again!

Last edited by ohmysql (2016-09-10 23:22:09)

Offline

Board footer

Powered by FluxBB