if [ "$PRESERVE_SSH_CWD" = "true" ]; then # Ensure the directory where we store terminal cwds and ssh exists mkdir -p $HOME/.config/terminalscwd/ # Alias for cd: when we change directory, we write the new cwd in the file corresponding to the terminal uuid cd() { if [ -z "$1" ]; then directory=$HOME elif [[ "$1" == "-P" ]] && [ -z $2 ]; then directory="$HOME" option="$1" elif [[ "$1" == "-P" ]]; then directory="$2" option="$1" else directory="$1" fi if [ -z "$option" ]; then builtin cd "$directory" else builtin cd "$option" "$directory" fi pwd > $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd } # Alias for ssh: when we ssh somewhere, we write it so that we know we should ssh when cloning terminal ssh() { echo $1 > $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh /usr/bin/ssh $@ rm $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh pwd > $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd } # If terminal uuid does not exit if [ -z $TERMINAL_UUID ]; then # Generate one export TERMINAL_UUID=$(uuidgen) fi # Copy state for new terminal if [ ! -z $PARENT_TERMINAL ]; then if [ -f $HOME/.config/terminalscwd/$PARENT_TERMINAL.cwd ]; then cp $HOME/.config/terminalscwd/$PARENT_TERMINAL.cwd $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd fi if [ -f $HOME/.config/terminalscwd/$PARENT_TERMINAL.ssh ]; then cp $HOME/.config/terminalscwd/$PARENT_TERMINAL.ssh $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh fi fi # When we're done, delete the uuid files preserve_ssh_cleanup() { rm -rf $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd rm -rf $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh } trap preserve_ssh_cleanup EXIT # If there already is a file corresponding to the current uuid if [ -f $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd ]; then # Go the the right directory cd "$(cat $HOME/.config/terminalscwd/$TERMINAL_UUID.cwd)" else # Use our custom alias to go to home, and set the file cwd cd fi # If we're running in an ssh session if [ -f $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh ]; then # Run the ssh command, and exec shell in the end so the terminal doesn't exit at end of ssh ssh $(cat $HOME/.config/terminalscwd/$TERMINAL_UUID.ssh) fi fi