I had a function inside .zshrc that I removed.

Now, when I try to source it, it indeed sources it, but doesn't remove the function that once was inside .zshrc from memory.

Is there a way to remove the function (now I believe in memory, in zsh namespace or something like that) without restarting my machine?

  • 1
    You don't need to restart your machine, obviously. exec zsh will get you a fresh shell in the same terminal, directory, environment, etc. – Gilles 'SO- stop being evil' Mar 8 '11 at 18:55

In zsh, you can remove a function with unhash -f functionname or unfunction functionname.

That doesn't automatically clear functions you've removed from a given startup file, though, because of course the shell doesn't remember where it got it from in the first place and attribute any special meaning to re-sourcing the same file. So you'll have to know what you want to forget.

Since you mention .bashrc in the subject: the bash equivalent is unset -f functioname

  • If a function is binded to a key (bindkey command), is it possible to remove it as well? – Somebody still uses you MS-DOS Mar 7 '11 at 21:34
  • 1
    @Somebody still uses you MS-DOS — if you remove the function without removing the key binding, you'll get No such shell function `function' when you press the key. But perhaps what you want to do is start your key definitions in your dotfile with bindkey -d, to reset them to their default state each time before setting up custom things. – mattdm Mar 7 '11 at 21:54

unhash -mf "*" can be used to clear all functions.
Since it will wipe out the standard zsh goodness functions you will need to reload /etc/zsh/zshrc.

I have the following at the beginning of my .zshrc:

# Clear all functions and aliases
unhash -mf "*"
unhash -ma "*"
. /etc/zsh/zshrc  # reload standard functions and aliases

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.