Tmux: easily change the default path for new windows

Tmux is a wonderful terminal multiplexer, a little newer and a little more fun than the GNU Screen that you may be used to. This article outlines a drop-in solution for changing the default path for new Tmux windows from within a Tmux session.

In Tmux the directory where you begin your session becomes the default path for new windows. Sometimes you night want to change this while still inside the session.

Before version 1.9

Versions of Tmux prior to 1.9 offered the ability to change this via setting a simple session option:

tmux set-option default-path /some/new/path

After 1.9

But after 1.9, although you can pass any path to the new-window command, the only remaining straightforward way to just change the default path for new windows in a session overall is to detach and reattach the session in the new directory.

Thatโ€™s too much of an interruption for me!

Some people juggle environment variables and send them to new-window to get around this, but I wanted less fuss in my tmux.conf so I wrote an external script to do all the work.

Essentially, the move is to make a new detached session and reattach the original session in the new directory inside of that session. Yes, this is awkward, but it’s not my fault! ๐Ÿคท

Solution

This script!

Use like:

tmux-chdir /some/new/directory

 

In case of any updates to this script, you can always check the live version in my dotfiles:

https://github.com/cspeterson/dotfiles/blob/master/.bin/tmux-chdir

 

UPDATE: From this reply in Reddit it seems there might be another way to do this. It doesn’t work for me, but here it is: