Fix missing Aliases when using sudo

2021-01-15

I just learned a nice fix for missing aliases when using sudo that I wanted to share with you.

This fix was found on askubuntu (opens new window) and it is so easy and elegant that everyone could have thought of.

Add this to your users ~/.bashrc, or ~/.zshrc file.

alias sudo='sudo '

⚠ Note the space at the end of the alias content 'sudo '. This will cause that not only sudo is parsed using the aliases, but also the next word.

Let's say you have the following aliases in your users .bashrc file.

alias vim='nvim'
alias sudo='sudo '

Then you can use your vim alias with sudo just as shown below.

sudo vim /etc/hosts

Nice and tight right? 😎