Created
July 18, 2023 09:21
-
-
Save enotodden/0d7c9a10aeb0d4951a5ac87e8a4ecf74 to your computer and use it in GitHub Desktop.
cds - cd with aliases
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
# This is a comment, only seperate-line comments are supported | |
mything /long/path/to/my/thing | |
foo /Users/myuser/bar | |
# When a path doesn't start with / it is mapped relative to $HOME: | |
myotherthing Downloads/foo/bar |
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
#### #### #### #### #### #### CDS - Quick cd #### #### #### #### #### #### #### | |
# | |
# cd to shortcuts mapped in ~/.cdsmap | |
# | |
# The shortcuts are defined with an alias and a path: | |
# | |
# # Comments on separate lines starting with # is allowed | |
# mything /long/path/to/eventually/find/mything | |
# down Downloads | |
# | |
# If the path does not start with a /, it will be interpreted as $HOME/<path> | |
# | |
# | |
function cdsmap() { | |
if [ -f $HOME/.cdsmap ]; then | |
cat ~/.cdsmap | column -t | |
fi | |
} | |
function cds() { | |
if [ $# -eq 0 ]; then | |
cdsmap | |
return | |
fi | |
if [ "$1" = "" ]; then | |
cdsmap | |
return | |
fi | |
if [ -f ~/.cdsmap ]; then | |
key=`echo $1 | cut -d '/' -f1` | |
# remain is the part after the alias. So if my alias name is foo, | |
# mapped to /Users/myuser/mydir, then foo/abc/somedir will map to | |
# /Users/myuser/mydir/abc/somedir | |
remain=`echo $1 | sed "s@^$key@@g" | cut -d '/' -f 2-` | |
# Read cdsmap, remove lines that start with # for comments, find the the key and output the value | |
cdto=`cat ~/.cdsmap | sed -e '/^[ \t]*#/d' | grep "^$key" | cut -d ' ' -f 2- | sed 's/^ *//g'` | |
# If the value does not start with a /, assume that it is relative to the home directory | |
# And prepend $HOME | |
if [[ "$cdto" != \/* ]]; then | |
cdto="$HOME/$cdto" | |
fi | |
echo "Checking $cdto/$remain" | |
# If the directory is empty, print error and dump the map | |
if [ ! -d "$cdto" ]; then | |
echo "Directory for $cdto not found" | |
cdsmap | |
return | |
fi | |
if [ "$cdto/$remain" = "/" ]; then | |
echo "No such directory or cds alias: $1" | |
cdsmap | |
return | |
else | |
# Use builtin cd in order to allow cds to be used in a cd alias without looping | |
# back on itself | |
builtin cd $cdto/$remain | |
fi | |
fi | |
} | |
# 'cs' for short | |
alias cs=cds | |
# Complete based on the cdsmap file | |
_complete_cds(){ | |
if [ -f ~/.cdsmap ]; then | |
reply=(`cat ~/.cdsmap | sed -e '/^[ \t]*#/d' | awk '{print $1}'`) | |
else | |
reply="" | |
fi | |
} | |
compctl -x 'p[1]' -K _complete_cds -- cds | |
compctl -x 'p[1]' -K _complete_cds -- cs | |
# Remap regular cd to use cds if the directory does not exist | |
function cd() { | |
if [ -d $@ ]; then | |
builtin cd $@ | |
else | |
cds $@ | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment