1

I am unable to update a function definition in my zsh dotfiles.

Specifically, I have this in my ~/.zshrc

unalias cd 2>/dev/null # Prevent infinite loops if sourcing this file again in the same session
logged_cd() {
    cd "$@"
    pwd > ~/.last_cd
}

alias "cd"="logged_cd" # keep track of most recent directory 

I want to update the logged_cd function

logged_cd() {
    cd "$@" && pwd > ~/.last_cd || return 1
}

So I updated my ~/.zshrc with the updated code, and did source ~/.zshrc but the old definition was still there.

I saw this one related question that suggested using unfunction which I tried, but that didn't work either.

enter image description here

Note that I also tried opening a new tab in my terminal so that zsh would be reloaded entirely but even in the new shell, it still is using the old function definition. Is something else going on here?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
rasen58
  • 4,672
  • 8
  • 39
  • 74
  • 3
    Are you sure there couldn't be another copy of the old definition elsewhere in the file, or in a different file that's sourced after it's complete? Defining a function _does_ overwrite any prior definition under the same name. – Charles Duffy Feb 20 '23 at 01:12
  • 2
    BTW, please don't use the bash tag for zsh questions. They're _very_ different, mutually-incompatible shells with deeply conflicting design philosophies. – Charles Duffy Feb 20 '23 at 01:14
  • 2
    Unrelated to your question: you [can call](https://stackoverflow.com/a/37542100/9307265) `builtin cd` instead of `cd` to avoid the infinite loop mentioned in the code comments. Also, take a look at the `$OLDPWD` and `$PWD` variables. – Gairfowl Feb 20 '23 at 05:24
  • Agreed; this question is mostly attempting to (poorly) reimplement existing functionality provided by `zsh`. – chepner Feb 20 '23 at 17:26

0 Answers0