1

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.

3
  • 1
    Just to make sure I understand the question correctly: you’re in a shell inside a tmux session, and you want to change that session "working directory" so that new windows will have the same working directory as the location where you’re running the script?
    – Immae
    Nov 17 '20 at 21:05
  • Correct. Or a directory passed as an argument, but defaulting to the working directory of the script.
    – Lucretiel
    Nov 17 '20 at 21:08
  • As a workaround (not sure if the direct way is feasible, I didn’t see how so far), would a solution like this be good for you? - in your script, do 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)
    – Immae
    Nov 17 '20 at 21:24
2

I think one solution would be:

tmux bind n new-window -c "/the/dir/you/want"

(Replace "n" by the actual character you use when creating a new window.)

If I understand correctly, you want that between the time you run my-script /some/dir and the time you run my-script /some/other/dir you want every new window to open in directory /some/dir.

The command I propose will just override the "new-window" binding you most likely use. I you run tmux new-window by hand everytime you need a new window, it won't work though.

(EDIT 1) Although, if you run tmux new-window from a script, it does not cost much to do tmux new-window -c "${the_dir:-(pwd)}".

3
  • I like this as a workaround. Would I need to do a similar rebinding for every command that creates a new tty? new-window, split horizontal, split vertical, etc?
    – Lucretiel
    Nov 17 '20 at 21:58
  • Yes. Actually, my .tmux.conf has these: bind h split-window -h -c "#{pane_current_path}" bind v split-window -v -c "#{pane_current_path}" but not for new-window. So when I split I keep the path. Not when I create a new window.
    – judu
    Nov 17 '20 at 22:09
  • Follow-up q: this wouldn't persist to future tmux sessions would it? Since I'm not editing the config file?
    – Lucretiel
    Dec 16 '20 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.