Skip to content

Instantly share code, notes, and snippets.

@jlong
Created June 30, 2011 18:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlong/1056902 to your computer and use it in GitHub Desktop.
Save jlong/1056902 to your computer and use it in GitHub Desktop.
Drop in .bash_profile to always remember the working directory between sessions
# Remember the current directory
if [ `type -t cd` == builtin ]; then
cd ()
{
builtin cd "$@"
pwd > ~/.working-directory
}
else
# Alias the cd function (needed if you run RVM)
eval "$(echo "__cd_without_cwd()"; declare -f cd | tail -n +2)"
cd ()
{
__cd_without_cwd "$@"
pwd > ~/.working-directory
}
fi
if [[ -e ~/.working-directory ]]; then cd $(cat ~/.working-directory); fi
@chriseppstein
Copy link

you know about cd - right?

@jlong
Copy link
Author

jlong commented Jul 1, 2011

That doesn't work between sessions. What this lets me do is cd into a project directory and open a couple of tabs in the same directory. It also remembers where I was when I open Terminal the next time.

What I realized was that the default behavior of always starting in the home directory when I create a new session is rarely what I want. A large percentage of the time I want to go back to whatever I was working on before. And cd ~ is easy enough to type if I need to actually be in my home directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment