Skip to content

Instantly share code, notes, and snippets.

@tilmanschweitzer
Created November 10, 2011 13:34
Show Gist options
  • Save tilmanschweitzer/1354859 to your computer and use it in GitHub Desktop.
Save tilmanschweitzer/1354859 to your computer and use it in GitHub Desktop.
Makes an alias for cd'ing to the current directory.
#!/bin/sh
###
# cdalias
###
# Makes an alias for cd'ing to the current directory.
# First parameter is alias name.
# If first parameter is -d followed by <alias-name> the matching alias will
# me deleted.
# Put this code in your ~/.bashrc or source this file accordingly.
######################################################
# ALIAS_DIR: for now, dont forget the ending /
ALIAS_DIR="~/"
# ALIAS FILE: leading dots not possible yet
ALIAS_FILE=".bashrc_aliases"
# source alias-file if exists
if [ -f $ALIAS_DIR$ALIAS_FILE ]; then
source $ALIAS_DIR$ALIAS_FILE
fi
function cdalias(){
alias_path=$ALIAS_DIR$ALIAS_FILE
dest=$(pwd)
alias_name=$1
if [ $1 = -d ]; then
# -d deletes alias
alias_name=$2
fi
if [ -d $ALIAS_DIR ]; then
if [ ! -f $alias_path ]; then
touch $alias_path
echo "#!/bin/sh" > $alias_path
fi
if [ $1 = -d ]; then
# just to be save
cp $alias_path $ALIAS_DIR$ALIAS_FILE.bak
rm $alias_path
awk '!/alias '$alias_name'=/{ print };' $alias_path.bak > $alias_path
else
echo "alias ${alias_name}=\"cd $dest\"" >> $alias_path
fi
source $ALIAS_DIR$ALIAS_FILE
else
echo "alias directory $ALIAS_DIR does not exist."
echo "Make sure to change your ALIAS_DIR or create the directory."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment