How to create a new window on the current directory in tmux?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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}"

Method 2

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.

Method 3

Yes, 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 -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"

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

I also answered in this duplicate question.

Method 4

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

tmux new-window -c "$PWD"

Method 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.

Method 6

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/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x