Skip to content

Instantly share code, notes, and snippets.

@leogtzr
Created January 12, 2020 01:11
Show Gist options
  • Save leogtzr/c0b046a4d38a8038c202fc361d9b98da to your computer and use it in GitHub Desktop.
Save leogtzr/c0b046a4d38a8038c202fc361d9b98da to your computer and use it in GitHub Desktop.
How to share your current working directories with other terminals ...
PROMPT_COMMAND=__prompt_command
__prompt_command() {
if [[ -f '/tmp/sdir' ]]; then
local -r terminal_number=$(tty)
local -r terminal_number_in_sdir_file=$(awk --field-separator '|' '{print $1}' /tmp/sdir)
local -r dir_in_sdir_file=$(awk --field-separator '|' '{print $2}' '/tmp/sdir')
if [[ "${terminal_number}" != "${terminal_number_in_sdir_file}" ]]; then
\rm --force '/tmp/sdir'
echo "Changing to: [${dir_in_sdir_file}] via 'sdir' file"
cd "${dir_in_sdir_file}"
fi
fi
}
# You will also need this bash function:
sdir() {
local -r terminal_number=$(tty)
local -r current_dir=$(readlink --canonicalize-existing "${PWD}")
echo "${terminal_number}|${current_dir}" > /tmp/sdir
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment