vmonteco
vmonteco

Reputation: 15463

Different starting directory per window?

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 :

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

Upvotes: 4

Views: 5769

Answers (2)

Wolph
Wolph

Reputation: 80111

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

Upvotes: 8

user268396
user268396

Reputation: 12006

Yes, it turns out the -c option to the new-window command is what you are looking for: https://unix.stackexchange.com/questions/12032/create-new-window-with-current-directory-in-tmux Also, this: https://unix.stackexchange.com/questions/101949/new-tmux-panes-go-to-the-same-directory-as-the-current-pane-new-tmux-windows-go

So either of tmux new-window -c $(pwd) or tmux new-window -c /path/to/dir inside your tmux session should do it.

Upvotes: 1

Related Questions