Files
q-tools/qcd_function
2013-02-22 10:11:51 +02:00

80 lines
2.1 KiB
Plaintext

function cd_history () {
if [ -z "$1" ]
then \cd "$HOME"
return
fi
old=$( tail -n 149 "$HOME/.bash_cdhistory" )
echo "$old" > "$HOME/.bash_cdhistory"
\cd "$1"
p=$( pwd )
b=$( basename "$p" )
echo $b":"$p >> "$HOME/.bash_cdhistory"
}
alias cd=cd_history
function qcd() {
while getopts alhm opt
do case "$opt" in
a)
# Adding
name=${!OPTIND}
if [ -z "$name" ]
then name=$( basename $( pwd ))
fi
echo "$name":$( pwd )
echo "$name":$( pwd ) >> ~/.qcd
;;
l)
echo ==History ~/.bash_cdhistory:
cat ~/.bash_cdhistory
echo
echo ==Saved ~/.qcd:
cat ~/.qcd
;;
m)
IFS=$'\n'
for line in $( cat ~/.bash_cdhistory );
do if [ -d "${line/*:/}" ];
then echo "$line" >> ~/.bash_cdhistory.tmp
fi
done
mv ~/.bash_cdhistory.tmp ~/.bash_cdhistory
for line in $( cat ~/.qcd );
do if [ -d "${line/*:/}" ];
then echo "$line" >> ~/.qcd.tmp
fi
done
mv ~/.qcd.tmp ~/.qcd
;;
h)
echo "qcd [-al] [name]"
echo "Change current working path based on the list ~/.qcd"
echo "Keeps a history of folders visited in ~/.bash_cdhistory"
echo " "
echo "qcd -a [name]"
echo " Adds the path to the list"
echo " You may add the name of the path, but when omitted"
echo " the basename will be used"
echo " "
echo "qcd -l "
echo " Lists the paths"
echo " "
echo "qcd -m "
echo " Maintain the list files, deleting non-existing entries"
;;
esac
done
unset OPTSTRING
unset OPTIND
if [ -z "$1" ]
then \cd
else
d=$( tac ~/.bash_cdhistory | grep -h ^"$1" ~/.qcd - | head -n 1 )
d=${d/*:/}
if [ ! -z "$d" ]
then \cd "$d"
fi
fi
}