Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different starting directory per window?

Tags:

tmux

I daily use tmux (2.5) on my laptop to work, and my tmux sessions have a starting directory which is the working directory I started the tmux session from. Every pane/window I open start with this starting directory as working directory.

I can change this starting directory, and this change would apply to the whole session.

But if I want to work on a different project with several panes, I could start a new window, but every pane I would open in it would start with the session's starting directory : I would have to cd to the new location for each pane which isn't practical.

If I need to work on several project/directories simultaneously, I can start a new terminal session, then cd to the relevant directory/project and start a new tmux session. That's not complicated.

But if I want to do the same thing on a server through ssh, I'd need to either :

  • open a new ssh session.
  • either embed my remote tmux sessions in an other tmux session.

Neither sounds practical to me, I'd prefer a single tmux session on the remote machine.

I think it would be more convenient to being able to start new window with its own starting directory location that would apply to any new pane opened in it. Is there a way to achieve this?

Edit :

I already tried the -c parameter of tmux new-window command.

But it doesn't assign its starting directory to the window created this way, it only applies this custom starting directory to the first pane created.

Any new pane opened in this window then uses the session's starting directory as default working dir (and not the path passed to tmux new-window).

like image 215
vmonteco Avatar asked Oct 23 '25 00:10

vmonteco


1 Answers

This question is very similar to: https://unix.stackexchange.com/questions/12032/create-new-window-with-current-directory-in-tmux

It depends on your tmux version but the -c parameter does do the trick but it does not remember the setting. There used to be a default-path setting but that has been removed in version 1.9 unfortunately.

For newer versions you will need to pass along the -c in all cases (you can use an alias if you manually execute that command) or if you use key bindings you need to rebind the split/new window keys.

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

To use a custom path instead of the current pane path, execute this command:

tmux setenv custom_path /home/whatever/some/path

Put this in your config:

bind '"' split-window -c "#{custom_path}"
bind % split-window -h -c "#{custom_path}"
bind c new-window -c "#{custom_path}"
like image 107
Wolph Avatar answered Oct 25 '25 14:10

Wolph