67

The directory where a tmux session is started in will be the directory that new windows start at. How do I change this starting directory without closing the tmux session?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Adnan
  • 814
  • 1
  • 6
  • 8
  • 1
    This question would be more on topic at http://superuser.com, and [this question & answers over at unix.stackexchange.com](http://unix.stackexchange.com/questions/12032/create-new-window-with-current-directory-in-tmux) may help you – Michael Berkowski Dec 05 '14 at 02:38
  • 3
    Below is the quickest way to do it from within the session. `:attach-session -c /my/path` – Chanaka Jun 17 '19 at 03:13

5 Answers5

69

The way to do this is to detach from the session (^b d with the default keybindings) and then specify a different directory when you reattach to it. When attaching to a session, use the -c flag to specify the working directory. Here's an example:

$ tmux list-sessions
tmuxwtfbbq: 3 windows (created Tue Apr  5 14:25:48 2016) [190x49]
$ tmux attach-session -t tmuxwtfbbq -c /home/chuck/new_default_directory

This setting will be persisted - after you've reset the working directory, you won't need to keep specifying it every time you reattach to the session.

For the record, I'm on tmux version 2.0 (though I don't think it matters - I couldn't find anything about adding a -c option to the attach-session command in the change logs so I assume it's been there for quite a while).

chucksmash
  • 5,777
  • 1
  • 32
  • 41
  • 1
    This was helpful for me. It seems the "current working directory" is associated with that particular attachment to the session (where you were when you ran tmux) and not the tmux session itself. Thanks chucksmash! – murftown Apr 12 '16 at 05:55
  • 2
    This also works when starting a new session. `tmux new -s foo -c ~/some/path`. I'm on version 2.1. – meh Aug 22 '17 at 14:38
  • 1
    It would be great if somebody knows if it's possible for e.g. `new-window` to use the cwd from the session. – Tom Nov 10 '20 at 02:11
  • It'd be nice if we could do something like `^b r` ("root") to define the starting directory to the current working directory for further windows/panes – Sumak Oct 27 '21 at 11:41
  • @Tom Do you mean to use the cwd of the current pane? new-window uses the cwd associated with the session. My answer has a binding using the cwd of the active pane to update the directory for the session and another to open a new window with that directory without changing the session directory. I find the latter super useful in my bindings for creating a new split pane, since I always want the same dir. – spazm Feb 18 '23 at 05:07
  • @Sumak my answer has a binding to update the session directory from the CWD of the current tmux pane. `bind M-c attach-session -c "#{pane_current_path}"` I bind prefix alt-c . – spazm Feb 18 '23 at 05:08
33

Chucksmash's answer is a good one, but it can also be achieved without using the session if you like. The command attach-session is also available in the tmux command prompt; and the target session can be specified as the "current" session using a dot.

attach-session -t . -c /path/to/new/directory
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Angelo
  • 786
  • 8
  • 8
  • 16
    You could also do below from within the session. `:attach-session -t . -c '#{pane_current_path}'` – Chanaka Jun 17 '19 at 03:06
  • 7
    This allows one to add a key-binding to update the working directory, such as using `bind -n M-u attach-session -t . -c '#{pane_current_path}'` to bind alt-u (without prefix) to such an update. – Alex W Mar 30 '20 at 12:29
  • 2
    `-t .` is not needed. `attach-session -c '#{pane_current_path}'` is enough. – Hari Feb 25 '23 at 12:53
9

use attach-session -c from command prompt or in a binding.

I have bindings to use the current directory automatically, using the provided pane_current_path var.

I have this bound to M-c, to update the current path.

# tmux.conf
# set default directory for new windows in this session to current directory:
bind M-c attach-session -c "#{pane_current_path}"

Similarly, when I just want another window in the current directory without changing the default, I have a binding on C:

# tmux.conf
# open a new window in the current directory
bind C new-window -c "#{pane_current_path}"

My bindings:

  • c: open a new window in session default directory [default binding]
  • C: open a new window in current directory
  • M-c: set session default directory to current directory

Happy Tmuxing!

spazm
  • 4,399
  • 31
  • 30
7

Here's how you can change the tmux session's working directory without detaching the session, and without needing use to the <prefix> keystrokes:

(Option 1) Enter the directory at tmux command prompt:

tmux command-prompt "attach -c %1"

...will open a command prompt, then you type the working directory you want ~/my/dir and press ENTER

(Option 2) Provide the directory on the in-pane command line:

# Execute this in one of the shell panes of within your tmux session:
tmux command-prompt -I $PWD -P "New session dir:" "attach -c %1"

With this approach, the prompt for new-directory is pre-populated with the current dir of the pane which launched the command. Of course you can substitute anything else for $PWD if you please.

Want a shell function?

I have added this to my shell initialization:

# Change the current directory for a tmux session, which determines
# the starting dir for new windows/panes:
function tmux-cwd {
    tmux command-prompt -I $PWD -P "New session dir:" "attach -c %1"
 }

With all of these options, any future new windows will start in the given dir.

Note: attach, attach-session, and a are all aliases for each other. The tmux command-prompt has many other powers, it's worth reading the man page

Stabledog
  • 3,110
  • 2
  • 32
  • 43
wisbucky
  • 33,218
  • 10
  • 150
  • 101
  • 1
    regarding Option 2: command-prompt -P option seems to require tmux version > 2.8. also you explicitly stated no need for `` but as a give-away: `:` and then `attach -c "#{pane_current_path}"` works also quite well. All kudos to https://unix.stackexchange.com/a/274551/348688 – nuala Aug 28 '20 at 23:48
  • 1
    you can use `tmux command-prompt "attach -c %1 $PWD"` and just hit enter to set the current directory as default in new panes. – ton Oct 10 '20 at 21:27
1

The default working directory can be changed from the command line like this:

TMUX= tmux -C attach -c directory -t session </dev/null >/dev/null

Compared to command-prompt, for example, this has the advantage that no interactive prompts or confirmations are produced, so this can be run in a script. Unsetting TMUX allows attaching from inside a session, the option -C turns on control mode for non-interactive use and redirecting from /dev/null causes the client to detach immediately, while still updating the default directory. Redirecting the output is not necessary, but it suppresses messages.

Juho Östman
  • 1,544
  • 1
  • 12
  • 20