Last active
January 31, 2022 17:28
-
-
Save pcdv/3c52d82f59fe9042793033d5e249b146 to your computer and use it in GitHub Desktop.
Directory bookmarks in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compact version