298

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.

Ciro Santilli OurBigBook.com
  • 18,092
  • 4
  • 117
  • 102
sharat87
  • 4,259

6 Answers6

417

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 tell Tmux it shouldn't start a string but rather bind the " key.

bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"
  • 1
    Does this not work on tmux 1.9? I can't get it to do the expected thing. – Erik Garrison Nov 17 '15 at 14:57
  • It should: https://github.com/tmux/tmux/blob/1.9/CHANGES#L12 I'd suggest using a more recent version, though – Simon Kohlmeyer Nov 20 '15 at 14:10
  • 3
    bind-key -r Enter new-window -c "#{pane_current_path}", works for tmux 2.1 – Marslo Jan 07 '16 at 10:12
  • 10
    Works in tmux 2.3 on OSX. Don't forget to stop / kill all existing sessions to see these bindings applied. – jmgarnier Mar 24 '17 at 10:19
  • 4
    @jmgarnier or re-source the profile: prefix-:, then type source ~/.tmux.conf – ijoseph Sep 05 '17 at 21:44
  • 13
    @jmgarnier or just tmux source-file .tmux.conf. – phil294 Oct 02 '17 at 21:35
  • Worked like a charm in June 2014. Still works on tmux 2.5 (tested on FreeBSD) but I fear it doesn't work anymore on tmux 2.5 (tested on Arch). I wonder if this is not linked to the way systemd opens a session. – Dereckson Oct 11 '17 at 07:42
  • @Dereckson It works for me with tmux 2.9_a-4 on Arch Linux. – Victor Nov 21 '19 at 13:08
  • I turned this into a TPM plugin if anyone finds that to be more convenient: https://github.com/openjck/tmux-keep-current-directory – John Karahalis Jul 12 '20 at 02:17
  • 1
    Using tmux 2.7 and this works perfectly. Thank you! For what it's worth, I personally find these bindings to be easier to remember for vertical and horizontal pane splits: bind / split-window -h -c "#{pane_current_path}" and bind - split-window -v -c "#{pane_current_path}" – Subfuzion Aug 14 '20 at 00:39
  • I am not sure why, but bind | split-window -h opens new pane under current directory, while bind | split-window -h -c "#{pane_current_path}" opens new pane under home directory. – zyy Feb 04 '22 at 12:28
  • That exactly I want. tmux 3.1c – EsmaeelE Sep 06 '22 at 07:49
188

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.

cigien
  • 103
  • 1
  • 4
Chris Johnsen
  • 20,101
  • 7
    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 :) – sharat87 Apr 27 '11 at 05: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. – sharat87 Apr 27 '11 at 05: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 07: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. – sharat87 Apr 27 '11 at 08:57
  • I was hoping this would be easier to add. I have been enjoying automatically switching to the current directory in iTerm2 and would love to find the same convenience in Tmux. – xer0x May 02 '11 at 23:03
  • @Chris: Is it possible to bind a key combination to tmux neww in .tmux.conf and is it possible to do something similar with new panes or sessions, as well as windows? – paradroid Aug 22 '11 at 22:20
  • 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 01:53
  • This post makes the situation look dire but the fact is that recent versions of tmux handle this perfectly, as indicated in the answer below. – Chris Hunt Apr 06 '18 at 01:15
  • 1
    That tmux FAQ URL no longer works, and the new one (https://github.com/tmux/tmux/wiki/FAQ) doesn't have that named How can I open a new window in the same directory as the current window?. Here's a mirror of the old one: https://github.com/ddollar/tmux/blob/d48eb68e5/FAQ#L330 – Max Barraclough Feb 04 '21 at 14:01
  • I bind 'C' to open another window in current dir using pane_current_path -> bind C new-window -c "#{pane_current_path}" see https://stackoverflow.com/a/68838778/117714 – spazm Aug 18 '21 at 20:20
68

Use new-window -c "#{pane_current_path}".

You can add the following to your ~/.tmux.conf to make it persistent (assumming default keybindings):

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

The default-path setting was removed from 1.9 (released on Feb 2014). In the change, the author recommended using either -c "#{pane_current_path}" or -c "$PWD in the new-window and split-window commands.

Also answered in this duplicate question.

mmoya
  • 6,008
12

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

tmux new-window -c "$PWD"
mislav
  • 381
5

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.

Anthon
  • 79,293
Erikw
  • 151
1

tmux did that in version 1.8 but in 1.9 this feature was removed in favor of using -c flag.

This can be solved but re-binding new-window but in case you want to run something else it becomes too wordy: instead of typing neww man tmux you'll have to type neww -c "#{pane_current_path}" man tmux which you most probably don't want to do.

There's a mod of tmux (I'm the author) to add a proper scripting language to tmux to allow using aliases, binding multiple commands in 'mode', variables, loops, etc... And also, it brings back the that behavior: new windows and panes are opened in the current directory.

It can be built from sources here: http://ershov.github.io/tmux/