4

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, 2020 at 21:05
  • Correct. Or a directory passed as an argument, but defaulting to the working directory of the script.
    – Lucretiel
    Nov 17, 2020 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, 2020 at 21:24

2 Answers 2

3

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, 2020 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, 2020 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, 2020 at 19:30
1

Until tmux version 3.1 it was possible to programmatically change the working directory from a shell within the tmux's session itself combining commands command-prompt and attach-session (or just attach/at). See the following bash session example:

$ pwd
/tmp/test
$ tmux new-session -t test
$ cd /home
$ tmux command-prompt -I "$PWD" -p "New session dir:" "attach -c%1"
$ tmux new-window
$ pwd
/home

n.b.: tmux command-prompt will populate the current client command prompt with the provided template, it is required a confirmation (ENTER) in order to execute the tmux's command prompt line.

Unfortunately on version 3.2 such command is broken if executed on the tmux session itself, but it still works if running from outside the session. As example, following the previous terminal session extract, when trying to change the working directory on version 3.2 it will produce the following error:

$ tmux command-prompt -I "$PWD" -p "New session dir:" "attach -c%1"
sessions should be nested with care, unset $TMUX to force

Despite all on version 3.2 the changing of working directory is still possible - as already was on tmux version 3.1 - with any of the following:

  1. when attaching to a session
  2. when using a tmux keybind command from within the session
  3. when manually triggering command prompt from within the session

Down here I'm still illustrating those alternatives just for answering completion, despite those are not what was desired to be archived. Yet I have not found any solution on tmux 3.2 in order to change the tmux working directory with just a shell command!

[1] By attaching to a new session, see following terminal session example:

$ pwd
/tmp/test
$ tmux new-session -t test
$ tmux detach-client
$ tmux attach -c /home -t test
$ tmux new-window
$ pwd
/home

[2] Instead the following example could be used into the tmux configuration ($HOME/tmux.conf) to change the working directory to the current pane current working directory with the keybind Alt+c:

bind M-c attach-session -c "#{pane_current_path}"

[3] For using tmux command prompt manually, just type the escape keybind (default to C-b) and then type :attach -c /new/working/dir/path followed by ENTER.

2
  • (0a) Shell variables (like $PWD) should always be quoted (in double quotes: "$PWD") unless you have a good reason not to, and you’re sure you know what you’re doing.  (0b) Constant strings (not containing variables) may be quoted with single quotes (e.g., 'New session dir:' and 'attach -c%1'), but, as long as they don’t contain $s, double quotes are OK. … (Cont’d) Jun 19, 2022 at 1:49
  • (Cont’d) …  I don’t understand.  (1) You show the same command twice.  Why?  (2) Can you explain how the command-prompt command works?  If it is run from outside a tmux session, how does it know which session to manipulate?  (3) The second time you show the command-prompt command, you decorate it with a Heavy Right-Pointing Angle Quotation Mark Ornament.  Why?  (4) Can you explain how the bind M-c part of the answer works? … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete. Jun 19, 2022 at 1:49

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.