Skip to content

Instantly share code, notes, and snippets.

@fsimonis
Last active February 16, 2024 12:40
Show Gist options
  • Save fsimonis/61d0841aae18aa347c6623d7c5bf0e86 to your computer and use it in GitHub Desktop.
Save fsimonis/61d0841aae18aa347c6623d7c5bf0e86 to your computer and use it in GitHub Desktop.
OhMyZsh plugin to stay in the same directory when opening a new terminal. Works with zoxide

persistent-term

This is a plugin for oh-my-zsh.

It keeps track of the last directory you cd to. When starting a new terminal, it automatically cds to that directory again. If zoxide is loaded, then the plugin uses __zoxide_z instead of cd.

This is very useful if you want to quickly start two terminals, one for a source directory and one for a build directory. You start in one terminal and cd to the source. Then you open a new terminal, which starts in the same directory an you cd to the build directory.

# save path on cd
function cd {
# Use zoxide if loaded. Works with --no-cmd
if type __zoxide_z > /dev/null ; then
__zoxide_z $@
else
builtin cd $@
fi
# Keep track of the directory
pwd > /tmp/last_dir
}
# On shell load, cd to the latest path
if [ -f /tmp/last_dir ]; then
DIR=$( cat /tmp/last_dir )
# Make sure the directory exists
if [ -d "$DIR" ]; then
builtin cd $DIR
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment