2

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

vmonteco
  • 14,136
  • 15
  • 55
  • 86

2 Answers2

5

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}"
Wolph
  • 78,177
  • 11
  • 137
  • 148
  • Wouldn't it use the working dir of the current pane rather than a starting dir assigned to the current window? That could be an acceptable alternative though. – vmonteco Jul 05 '17 at 22:24
  • @vmonteco yes it is, if you wish to maintain the assigned directory I think you need to assign it to some environment variable. I'm still experimenting with that (I'd like it as well) but haven't found a working solution yet – Wolph Jul 05 '17 at 22:29
  • @vmonteco I've updated the answer to include both options :) – Wolph Jul 05 '17 at 22:58
  • Thank you, but wouldn't this second solution do the same as just changing default starting directory? – vmonteco Jul 05 '17 at 23:40
  • @vmonteco the setting should be session specific unless you use `setenv -g` to set it globally, here's the manual for reference: http://man.openbsd.org/OpenBSD-current/man1/tmux.1#set-environment – Wolph Jul 06 '17 at 01:43
  • Yes, but I don't get how it's more convenient than directly using the default starting directory if it's session specific and not window specific. – vmonteco Jul 06 '17 at 03:21
1

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.

user268396
  • 11,576
  • 2
  • 31
  • 26
  • 1
    I already tried this, but the specified path only applies on the first pane created. Any new pane is created with the session's starting directoy. It doesn't assign its own starting directory to the new window. – vmonteco Jul 05 '17 at 21:46
  • Erratum : A way to use the current working dir for a new pane could do the trick. I'm considering this and studying the references you linked. – vmonteco Jul 05 '17 at 21:57