Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is is possible to open a new-window with its working directory set to the one I am currently in. I am using zsh, if it matters.

share|improve this question

5 Answers 5

up vote 51 down vote accepted

Starting in tmux 1.9 the default-path option was removed, so you need to use the -c option with new-window, and split-window (e.g. by rebinding the c, ", and % bindings to include
-c '#{pane_current_path}'). See some of the other answers to this question for details.


A relevant feature landed in the tmux SVN trunk in early February 2012. In tmux builds that include this code, tmux key bindings that invoke new-window will create new a window with the same current working directory as the current pane’s active processes (as long as the default-path session option is empty; it is by default). The same is true for the pane created by the split-window command when it is invoked via a binding.

This uses special platform-specific code, so only certain OSes are supported at this time: Darwin (OS X), FreeBSD, Linux, OpenBSD, and Solaris.

This should be available in the next release of tmux (1.7?).


With tmux 1.4, I usually just use

tmux neww

in a shell that already has the desired current working directory.

If, however, I anticipate needing to create many windows with the same current working directory (or I want to be able to start them with the usual <prefix>c key binding), then I set the default-path session option via

tmux set-option default-path "$PWD"

in a shell that already has the desired current working directory (though you could obviously do it from any directory and just specify the value instead).

If default-path is set to a non-empty value, its value will be used instead of “inheriting” the current working directory from command-line invocations of tmux neww.

The tmux FAQ has an entry titled “How can I open a new window in the same directory as the current window?” that describes another approach; it is a bit convoluted though.

share|improve this answer
1  
Is there a way I can map <prefix>c to read the working directory of the underlying shell instance (if any) and set the default-path prior to executing new-window. Or is that too much to ask of tmux :) –  Shrikant Sharat Apr 27 '11 at 5:34
    
On another note, is it even possible to read the underlying shell's working directory? I'd kill to have it displayed in my status bar. –  Shrikant Sharat Apr 27 '11 at 5:34
2  
There is no portable way to extract the cwd of another process (though it is possible on some platforms (e.g. /proc/PID/cwd on Linux)). There is a possible partial solution in an entry of the tmux FAQ (it has the shell record its cwd when it prints a prompt, then binds a key that starts a new shell in the recorded directory). –  Chris Johnsen Apr 27 '11 at 7:14
    
ok, this is a bit out of scope for my knowledge and doesn't feel very reliable. Something tells me I may be better off without all this.. thanks anyway. –  Shrikant Sharat Apr 27 '11 at 8:57
1  
@paradroid: Anything done via a binding will (by default) use the cwd of the tmux server or the value of the default-path session option (if that is set). The tmux FAQ has an entry that describes a way to bind a key that starts a new window with the cwd of the shell running in the current window (“How can I open a new window in the same directory as the current window?”), but the method is quite convoluted. The same could probably be done for split-window and new-session (instead of neww). –  Chris Johnsen Aug 23 '11 at 1:53

With recent versions of tmux (v1.8, but maybe in v1.7 too):

tmux new-window -c "$PWD"
share|improve this answer
    
This appears to work with split-window as well, e.g. tmux split-window -v -c "$PWD" –  user7089 Nov 5 '13 at 17:53
    
$PWD doesn't appear to work for me in tmux 1.9a. I had to use pane_current_path as suggested above. –  jordelver Oct 16 at 13:39

The other answers does not work for me when I try put them as bindings (specifically tmux split-window -c). But I've made up my own solution that I've been using for more than a year that works for both new-window and splits:

~/.bashrc:

PS1="$PS1"'$([ -n "$TMUX" ] && tmux setenv TMUXPWD_$(tmux display -p "#D" | tr -d %) "$PWD")'

~/.tmux.conf:

unbind-key c
bind-key c run-shell 'tmux new-window "cd \"$(tmux show-environment $(echo "TMUXPWD_#D" | tr -d %) | sed -e "s/^.*=//")\"; exec $SHELL"'
bind-key C new-window

bind-key - run-shell 'tmux split-window -v "cd \"$(tmux show-environment $(echo "TMUXPWD_#D" | tr -d %) | sed -e "s/^.*=//")\"; exec $SHELL"'
bind-key | run-shell 'tmux split-window -h "cd \"$(tmux show-environment $(echo "TMUXPWD_#D" | tr -d %) | sed -e "s/^.*=//")\"; exec $SHELL"

Works, at least, with $(tmux -V) 1.8. See out-commented lines here for a version working for older tmuxes that don't have the show-environment command.

share|improve this answer

The safest option is in mislav's answer: add -c "$PWD" to the new-window and (optionally) split-window commands.

The default-path path setting was removed from upstream code and tmux author recommended in that commit message using either -c "$PWD or -c "#{pane_current_path}" in the new-window or split-window commands.

I'm using git master (tmux reports version 1.9) and both options works for me.

There is also another upstream commit removing pane_current_path from the manpage so it might not be available in the future.

In summary:

  • default-path won't be available in post 1.8 versions.
  • pane_current_path might not be available in post 1.8 versions.

Disclaimer: Added this for the sake of completeness, I first answered here but that question is an obvious duplicate of this one.

share|improve this answer

The current (1.9a) tmux man page lists an optional -c start-directory parameter for some commands, including new-window and split-window. It also contains the Format variable pane_current_path, which refers to the Current path if available.

By combining these, we can open a new window with the current working directory using
new-window -c "#{pane_current_path}"
The quotation are needed in case the current path contains spaces.

If you want to split the current pane vertically, use
split-window -c "#{pane_current_path}"
or, for a horizontal split
split-window -h -c "#{pane_current_path}"

To make the key bindings open new splits and windows with the current working directory by default, add the following to your .tmux.conf. The " with surrounding quotes is to assure tmux it doesn't start a string but means the "-Key instead.

bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"
share|improve this answer
    
This is too much for a comment anyway. Can you highlight what is new/different in your answer, so it is clear what the new/added value is? –  Anthon Mar 6 at 9:54
    
Your answer is not easy to understand because split-/new-window combines two things in a non standard syntax, write each example out in full and explain what the -c does, and why your solution works. I would need to go to the tmux documentation in order to understand what you meant, which doesn't help us here. –  X Tian Mar 6 at 10:43
    
I rewrote the answer to be more focused on the problem, please tell me if you think I should improve it further. What I meant to comment was that $PWD will expand to the directory where tmux was started, and that the pane_current_path variable exists in tmux 1.9a. –  Simon Kohlmeyer Mar 6 at 18:23
    
It's just brilliant! –  A.B Mar 17 at 5:33

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.