I want to create a shell command that, when run, sets the working directory of the current tmux session to the current working directory.
There are numerous answered questions on how to set the session working directory; the answer seems to be to run
attach-session -t . -c /path/to/directory in the tmux command prompt. Under my (default) configuration, that'd be
C-b : attach-session -t . -c /path/to/directory.
I don't want to manually interact with the tmux command prompt; I want a simple command, mostly because I don't want to deal with typing out or copy-pasting the target directory. I know it's possible to send commands to tmux from a shell prompt; for instance, I can run
tmux display-message MESSAGE at the shell instead of
C-b : display-message HELLO to show a message in the status bar. However, running
tmux attach-session -t (tmux display-message -p '#S') -c (pwd) throws an error: "sessions should be nested with care, unset $TMUX to force". Of course, I don't have any desire to nest a tmux session, I only want to change the directory of the current session.
I've also tried using
tmux send-keys and
tmux-send-prefix, but these seem to forward the key sequences to the underlying tty (where the shell responds to them) rather than directly to tmux.
I'm using the fish shell, but I'm assuming the solution would fit in a shell script, so I'm happy with a solution in any language.
tmux update-environment -t . MY_WORKING_DIRECTORY /path/to/directory- At the end of your fish/bash/whatever initialisation script,
if [ -n "$MY_WORKING_DIRECTORY" ]; then cd "$MY_WORKING_DIRECTORY"; fi(bash syntax, I don’t know fish at all)