In my current zsh behaviour, hitting tab after typing "sudo" just adds a space.

$ sudo ...

What I really want is for the tab to fully expand to the path of the binary it is about to execute when I hit tab, allowing me to see what will be run:

$ /usr/bin/sudo ...

The aim is to protect against the case where the local user on my machine is hacked, an entry added to my $PATH variable, and 'sudo' runs a malicious program, which would be an easy escalation to root.

  • There is another solution which does not involve rebinding tab. Prepend the command name with = and press tab after the command, and it will expand the command name into full path. Feb 22 '13 at 10:46

Use bindkey builtin command to bind keys to Zsh commands, like this:

bindkey "^I" expand-cmd-path 

where "^I" is Tab. You can just drop this line into your ~/.zshrc file. Warning: it will break autocompletion of arguments.

  • Cheers! Clearly this is not an ideal solution so I've bound it to Ctrl-O instead. However there aren't many binaries that take password inputs (sudo, ssh, gpg, ...), so I'm happy to just hit Ctrl-O whenever I'm using one of those.I should also point out that this doesn't solve the entire problem since anyone with access to my account can subvert it via .zshrc or xmonad.hs, but gets me some of the way there!
    – Ali
    Jan 28 '12 at 20:04

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.