7

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

3 Answers 3

21

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}"
6
  • 1
    THIS is the correct answer. Specifically the 2nd option in my case. Mar 1, 2014 at 10:51
  • as a aside, don't forget to close all current working tmux terminals including background before chaning your configuration file otherwise tmux won't load new configurations. Mar 4, 2014 at 20:22
  • @lightmanhk You should be able to just :source ~/.tmux.conf Mar 7, 2014 at 2:40
  • 1
    Any way to generalize this for pane splits? split-window -h "#{pane_current_path}" won't work...
    – Bach
    Apr 22, 2014 at 10:58
  • 1
    @Bach Try with a -c. split-window -h -c "#{pane_current_path}" Apr 22, 2014 at 13:38
3

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 -
0

there are quite a few answers here: https://unix.stackexchange.com/questions/12032/create-new-window-with-current-directory-in-tmux

in summary:

from the tmux FAQ, one obtuse (but very general and fairly shell independent) way:

  • How can I open a new window in the same directory as the current window?

One option is to just run "TMUX= tmux" in the window. However, this only works if no command is running, so that you can input the command.

A workaround is to let tmux know about the current path through an environment variable. To do so, use the following command:

[ -n "$TMUX" ] && tmux setenv TMUXPWD_$(tmux display -p "#I") $PWD

Which sets TMUXPWD_i (where i is the number of the current window) to the path of the current directory. This command can be added to PS1, for example:

PS1='$([ -n "$TMUX" ] && tmux setenv TMUXPWD_$(tmux display -p "#I") $PWD)\h$ '

When a new window is created, the shell should be asked to change directory. You can define a new binding (for example, if using GNU bash):

bind-key C-c run-shell 'tmux neww "cd $(tmux display -p "\$TMUXPWD_#I"); exec bash"'

This solution will work even if a command is currently running in the terminal, but it will not work from a window that has just been swapped with another because TMUXPWD_i will not be updated after a swap. However, once a new prompt is displayed, TMUXPWD_i is updated properly.

simply replace calls to the shell as appropriate for yours.

also, apparently newer versions of tmux have this addressed with a command

tmux new-window

older versions are claimed to work thusly:

calling

tmux neww

from your shell will open tmux in that dir.

if you would like to be able to open new windows or panes from the current working directory in tmux then you should instead:

tmux set-option default-path "$PWD"

2
  • Thanks for trying to help, walrus. I saw that question on unix.stackexchange and read FAQ. However, these are not the answers for my question.
    – Shamaoke
    Oct 24, 2012 at 7:54
  • 1
    Look, when you create a new session, the current directory for every new window or pane you open in that session will be that directory where you start the session. This is true if I create a single session. However, when I open a new terminal window and start another one session in it the current working directory for every new window will be that directory where I start the first session but not the current one. This is apparently a bug in tmux 1.6 since when I downgraded it to version 1.3 the bug didn't appear.
    – Shamaoke
    Oct 24, 2012 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .