bin/tp

47 lines
1.0 KiB
Bash
Executable File

#!/bin/sh
# tp - teleport to frequently used directories
# usage:
# $ tp [ add | rm | $location ]
# `tp add $location' associates the current working directory with $location.
# `tp rm $location' removes the first occurence of $location.
# `tp $location' creates a new shell and places the user here. To get back to
# where you started, exit the newly created shell.
# ~/.tp contains all locations.
TP_DIRS="$HOME/.tp"
first_occurence() {
grep -m 1 "^$1\ " $TP_DIRS
}
teleport() {
if first_occurence $1 >/dev/null; then
cd "$(first_occurence $1 | awk '{print $2}')"
$SHELL
fi
}
add_new_directory() {
output=$(mktemp)
echo "$1" "$(pwd)" | cat - $TP_DIRS > $output
mv $output $TP_DIRS
}
remove_directory() {
# can't grep -v > file in place
output=$(mktemp)
grep -v -m 1 "^$1\ " $TP_DIRS > $output
mv $output $TP_DIRS
}
case "$1" in
add) shift
add_new_directory "$1"
;;
rm) shift
remove_directory "$1"
;;
*) teleport "$1" ;;
esac