Sample Header Ad - 728x90

How to change the tmux working directory with a shell command

4 votes
2 answers
6059 views
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.
Asked by Lucretiel (212 rep)
Nov 11, 2020, 09:10 PM
Last activity: Jun 20, 2022, 12:04 PM