Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

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 -c "#{pane_current_path}"
bind-key '"' split-window -h -c "#{pane_current_path}"
share|improve this answer
    
THIS is the correct answer. Specifically the 2nd option in my case. –  Michael Rapadas Mar 1 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. –  lightmanhk Mar 4 at 20:22
    
@lightmanhk You should be able to just :source ~/.tmux.conf –  David C. Bishop Mar 7 at 2:40
1  
Any way to generalize this for pane splits? split-window -h "#{pane_current_path}" won't work... –  Bach Apr 22 at 10:58
1  
@Bach Try with a -c. split-window -h -c "#{pane_current_path}" –  David C. Bishop Apr 22 at 13:38

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 -
share|improve this answer

there are quite a few answers here: http://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"

share|improve this answer
    
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 '12 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 '12 at 16:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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