Zsh – Fully Expand Binary Path on Tab Autocomplete

autocompletepathzsh

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.

Best Answer

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.

Related Question