Search code examples
command-linefish

fish shell directory stack


In zsh there was an option for pushing each visited directory automatically to the directory stack (setopt auto_pushd), represented by dirs. I just switched to the fish and recognized that there is no such default option. Also when trying to access dirs it is just filled with the current output of pwd. Is there a possibility to enable such kind of behavior in fish?

Of course there would be the option to write an own cd function in order to call pushd every time you cd into a folder, but as I'm also switching directories just using the path as command that would be not that useful.

So basically can I call a function every time my current working directory has changed?


Solution

  • You can run a function every time the directory changes, like so:

    function myfunc --on-variable PWD
        echo Changed!
    end
    

    However! fish's cd is by default a function wrapper that manages a directory stack. (You can see its guts by running functions cd.)

    You can use prevd and nextd to move backwards and forwards through your directory history. There is also the shorthand cd - to jump to the last directory you were in.