Tmux forgets the directory where the session was created

When I create multiple sessions in tmux, the current directory for every new window in subsequent sessions is set as the current directory for the first one.

For example:

-> cd /home/one && tmux new-session -n one\; new-window -n two
   # /home/one is the current directory for each window

-> cd /home/two && tmux new-session -n three\; new-window -n four #\; etc.
   # "/home/two" is the current directory for the window "three"
   # but for the window "four" and other created windows it is "/home/one"

How can I force tmux to set the current directory for every new window in subsequent sessions as the directory where the session was created?

tmux 1.6

zsh 4.3


Solution 1:

As of tmux 1.9 the default-path option has been removed.

You can use the following to open in the directory that the session opened in (the 'client' in the name would indicate to me that it's the current tmux runnin in your shell, but it seems to switch the paths when switching between sessions in the same client):

bind-key c  new-window -c "#{client_cwd}"

Otherwise to copy the path of the current pane:

bind-key c  new-window -c "#{pane_current_path}"

There is also pane_start_path which might appeal to some workflows.

bind-key c  new-window -c "#{pane_start_path}"

For the split window commands.

bind-key % split-window -h -c "#{pane_current_path}"
bind-key '"' split-window -c "#{pane_current_path}"

Solution 2:

I realize that this question is pretty old, but it came up as one of the only questions on StackExchange when I was looking for the answer myself, so here is how I solved it for tmux 1.8.

The new-window command takes -c flag that will allow you to specify the current directory of the new window. By default, it is an empty string, which is going to use the current panes working directory as the directory for the new window. Passing in a - as the value for the -c flag will set the directory of the new window to what the session was opened up in.

Here is a excerpt from the docs:

-c specifies the working directory in which the new window is created.  It may 
have an absolute path or one of the following values (or a subdirectory):

       Empty string    Current pane's directory
       ~               User's home directory
       -               Where session was started
       .               Where server was started

If you want to change the default behavior of the <PREFIX> c keybinding to reflect this, here is what I did in my ~/.tmux.conf:

bind-key c   new-window -c -