Skip to content

Instantly share code, notes, and snippets.

@miyl
Last active March 18, 2021 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miyl/b52a716e93990f3205ab02da4c169fc1 to your computer and use it in GitHub Desktop.
Save miyl/b52a716e93990f3205ab02da4c169fc1 to your computer and use it in GitHub Desktop.
Intelligent cd
# /usr/bin/env sh
# You've heard of exa/lsd, sd and fd. But what about a modern cd?
# This isn't really it. Try autojump/z/fasd?
# But maybe this does make plain old cd 5% better, on a good day?
# Description: Just a vaguely intelligent cd
# Specifically it can handle cd'ing to a path even if it has a filename
# at the end, in which case it cd's to the directory which contains it
cd() {
[ -z $1 ] && builtin cd
if [ "$(file --brief --dereference "$1")" != "directory" ]; then
builtin cd "$(dirname "$1")"
else
builtin cd "$1"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment