Skip to content

Instantly share code, notes, and snippets.

@hal0thane
Forked from defunkt/gist:132456
Created July 6, 2009 21:26
Show Gist options
  • Save hal0thane/141691 to your computer and use it in GitHub Desktop.
Save hal0thane/141691 to your computer and use it in GitHub Desktop.
MAJOR overhaul with per-tty saving and logout autosave
#!/usr/bin/env bash
# Bash snippet to open new shells in most recently visited dir.
# Useful if you want to open a new terminal tab at the present
# tab's location.
# Put this in your .bash_logout ONLY!
# At logout, remember the PWD, and switch there on next login
# Set TERMS to 1 if you wish to remember the path separately for
# each terminal or terminal instance used. So, for example, tty1's
# recalled path would only be the last path used on tty1, and the same
# for tty2, tty3, etc.
#
# If TERMS is set to 0, then only one recalled path will be used globally
# for all terminals
TERMS=1
MEMPATHDIR="${TMPDIR:-~/.temp}/mempath-$(logname)/"
##############################################################################
thisHost="$(hostname -s)"
function savepath () {
if [[ "x${TERMS}" == "x1" ]]; then
# using tty-specific path
thisTty="$(tty | xargs basename | sed -e 's,tty,,')"
else
# not using tty-specific path
thisTty=""
fi
thisPwd="$(pwd -P)"
if [[ ! -d $MEMPATHDIR ]]; then
mkdir -p "$MEMPATHDIR"
fi
# store it and quit
echo "$thisPwd" > "${MEMPATHDIR}/${thisHost}${thisTty:+.$thisTty}"
}
function recallpath () {
thisTty="$(tty | xargs basename | sed -e 's,tty,,')"
if [[ -e "${MEMPATHDIR}/${thisHost}.${thisTty}" ]]; then
thisPwd="$(/bin/cat ${MEMPATHDIR}/${thisHost}.${thisTty})"
elif [[ -e "${MEMPATHDIR}/${thisHost}" ]]; then
thisPwd="$(/bin/cat ${MEMPATHDIR}/${thisHost})"
fi
if [[ -n "$thisPwd" ]]; then
cd "$thisPwd"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment