2

When I call alias in a zsh shell, I get a list of aliases. Some of them are desired, some are not. I originally created a .alias file in my home directory, and added source $HOME/.alias to my .zshrc. How do I cull any existing alias that is not part of the file being sourced by my .zshrc? (Presumably I could do this one by one with unalias, but that seems incredibly cumbersome.)

2

unalias supports unsetting multiple aliases at the same time, so you do not have to do this one by one, necessarily.

I would suggest just dropping all aliases and reload them anew from ~/.alias.

unalias ${(k)aliases} ${(k)galiases} ${(k)saliases}; source 

This requires the zsh/parameter module to be loaded. Check with zmodload | grep parameter, load with zmodload zsh/parameter.

The parameters aliases, galiases and saliases map the names of regular, global and suffix aliases respectively to their expansions. ${(k)parameter} is substituted by the keys of parameter.


If you cannot load the module for some reason, you can work around it by using the output of the alias command:

unalias $(alias | cut -d = -f 1) ; source ~/.alias

If you only want to unset a specific kind of alias, use alias -r for regular aliases (those used instead of commands), alias -g for global aliases and alias -s for suffix aliases.


If you need to do this more often, just define a function in your ~/.zshrc (or some other file you source from there)

reload-aliases () {
    unalias ${(k)aliases}
    source ~/.alias
}
3
  • Awesome; that's exactly what I needed. Just had to reload zsh . ~/.zshrc, and problem solved. I would upvote, but my reputation isn't high enough. – b_archer Aug 13 '14 at 5:53
  • 2
    A much more zsh'ish way of removing all aliases is: unalias ${(k)aliases}, which does not neet an external command. – mpy Aug 13 '14 at 17:35
  • @mpy Thank you for the information on the aliases parameter. I incorporated it into my answer. – Adaephon Aug 14 '14 at 6:06

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.