Skip to content

Instantly share code, notes, and snippets.

@pcdv
Last active January 31, 2022 17:28
Show Gist options
  • Save pcdv/3c52d82f59fe9042793033d5e249b146 to your computer and use it in GitHub Desktop.
Save pcdv/3c52d82f59fe9042793033d5e249b146 to your computer and use it in GitHub Desktop.
Directory bookmarks in Bash
# https://gist.github.com/pcdv
function g() {
local HLP="Bookmark your favorite directories:
g : list bookmarked dirs
g . : add current dir to bookmarks
g -e : edit bookmarks
g <num> : jump to n-th dir
g <regex> : jump to 1st matching dir"
local D=_; local CFG="$HOME/.cdirs"
case $1 in
"") [ -f "$CFG" ] && nl "$CFG" || echo "$HLP" ;;
.) pwd >> "$CFG" ;;
-e) ${EDITOR:-vi} "$CFG" ;;
-*) echo "$HLP" ;;
[1-9]*) D=$(sed -ne "${1}p" "$CFG") ;;
*) D=$(grep "$1" "$CFG" | head -1) ;;
esac
if [ "$D" != _ ]; then
[ -d "$D" ] && cd "$D" || echo "Not found"
fi
}
@pcdv
Copy link
Author

pcdv commented Jan 31, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment