How do I use sudo with alias in zsh?

Say I have these two in my .zshrc:

alias sudo='nocorrect sudo'
alias boot27='grub-reboot 4 && reboot'

boot27 gives boot27: command not found. If I change the alias to 'nocorrect sudo ' or just 'sudo ', it works, but then other things like mkdir give nocorrect: command not found.


Just to clarify, you run sudo boot27 and get boot27: command not found because since sudo is an alias, bash stops scanning for aliases and does not recognize boot27 as one. As discussed here, one way of dealing with this is adding a space at the end of the alias definition.

Adding the space allows bash to recognize boot27 but the problem now is that your root account is not set to use zsh so sudo starts a bash shell instead. nocorrect is a zsh thing, bash has no idea what it is so it complains.

The simplest way to fix this would be to set root's shell to /bin/zsh:

sudo chsh

Then set your alias with the space:

alias sudo='nocorrect sudo '

Your boot27 alias has another problem though. I assume you run it as sudo boot27, the shell will read the alias and expand that to:

sudo grub-reboot 4 && reboot

So, it will run grub-reboot as root but it will run reboot as a normal user. What you want is either to change your alias to

alias boot27='grub-reboot 4 && sudo reboot'

and run it as sudo boot27, or to change your alias to

alias boot27='sudo grub-reboot 4 && sudo reboot'

and run it without sudo, just boot27.