Skip to content

Instantly share code, notes, and snippets.

@Rob--W
Created March 24, 2014 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rob--W/9738549 to your computer and use it in GitHub Desktop.
Save Rob--W/9738549 to your computer and use it in GitHub Desktop.
Go to current directory when you open screen or a new tab in screen.
#!/bin/bash
# The file where the PWD is going to be shared with other (new) tabs.
STY_SHARED_PWD="/tmp/.screenpwd-$STY"
if [ -e "$STY_SHARED_PWD" ] ; then
# If the file exists, then another session had already set the
# environment, so read the most recent PWD and go to the directory.
STY_PWD="$(</tmp/.screenpwd-$STY)"
# Provided that it's not empty.
[ -n "$STY_PWD" ] && cd "$STY_PWD"
fi
# Always write the latest location to the shared file.
export PROMPT_COMMAND="echo -n \"\$PWD\" > \"$STY_SHARED_PWD\""
# Start the real shell
export SHELL=bash
bash "$@"
# When the screen exits, remove the temporary file.
[[ -e "$STY_SHARED_PWD" ]] && rm "$STY_SHARED_PWD"
# Wrapped shell to ensure that PWD is always the latest PWD
shell $HOME/.screen.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment