DEV Community

John McGuin
John McGuin

Posted on • Originally published at blog.waysoftware.dev on

Time Travel Directories

The first time I saw somebody use cd - my mind was blown. If it's new to you, cd - will change to your previous directory. The shell is tracking a stack of recent directories, which can be seen via the builtin dirs command. You can also directly interface with the directory stack via pushdand popd. This is so cool and it's the backing mechanism that cd - uses. With that new knowledge, I created a small zsh function to help me time travel to any directory on the stack, instead of just the most recent.

function travel() {
    local options=()
    while read -r dir; do
        options+=("$dir")
    done < <(dirs -p)

    select dir in "${options[@]}"; do test -n "$dir" && break; echo "Invalid Selection"; done
    eval cd "$dir"
}

Enter fullscreen mode Exit fullscreen mode

When this function gets invoked, you're prompted to select an option from the directories on the stack and it will navigate back to it.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay