Skip to content

Instantly share code, notes, and snippets.

@norpol
Last active June 14, 2018 11:26
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 norpol/c26fae8dd293f303b7d2cc7454bdb0fa to your computer and use it in GitHub Desktop.
Save norpol/c26fae8dd293f303b7d2cc7454bdb0fa to your computer and use it in GitHub Desktop.
Share current pwd across multiple shells
# Put this into your ~/.zshrc
# Enables chpwd, which runs a command if you cd/pushd/popd...
# Stores your latest pwd into a file and automatically
# cd's into it in case you start another shell
# check also this out https://gist.github.com/norpol/1ff30f0f614c38dccfabb4f9f62a73e6
# you might get this working in bash too https://stackoverflow.com/questions/3276247/is-there-a-hook-in-bash-to-find-out-when-the-cwd-changes
store_pwd() {
loc="$(pwd)"
target="/var/run/user/${UID}/cur_dir"
# use tilde for any path in HOME
# useful if you want to show your current working dir
# in i3status bar for example
if [ "${loc#${HOME}}" != "${loc}" ]; then
echo "~${loc#${HOME}}" > "${target}"
else
echo "${loc}" > "${target}"
fi
}
cd_latest() {
target="/var/run/user/${UID}/cur_dir"
if [ -f "${target}" ]; then
loc="$(cat "${target}")"
if [ "${loc#\~}" != "${loc}" ]; then
loc="${HOME}${loc#\~}"
fi
cd "${loc}" 2>/dev/null || rm "${target}"
fi
}
chpwd() { store_pwd }
cd_latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment