1

If I create a new pane B from pane A and I'm not running any commands in A then B gets created with the same pwd as A which is what I want. However if while creating the new pane I'm running a bash command (an alias of 3 chained commands actually) in A that temporarily changes pwd then B gets created in whichever pwd is active in A at the time I press the key shortcuts instead of the pwd I am running the command from.

1) Is there a way of forcing tmux to create a new pane using the pwd before running such command? 2) If 1) isn't possible what's the right way of running the command so that the pwd is preserved?

I'm using tmux 1.8 with this .tmux.conf:

https://github.com/slnc/dotfiles/blob/master/.tmux.conf

Thanks

2

just provide the directory you want manually when you create the split

:split-window -c "/dir/you/want"

e.g.

<prefix>,:, split-window -c "/var/lib/apt"

Explanation

split-window is the tmux command to create a split, it takes alot of options to allow you to specify size, string interpolations as well as -c to specify working directory.

from man tmux | less '+/^\s*split-window'

 split-window [-bdhvP] [-c start-directory] [-l size | -p percentage] [-t target-pane] [shell-command] [-F format]
               (alias: splitw)

Create a new pane by splitting target-pane:
-h does a horizontal split and -v a vertical split; if neither is specified, -v is assumed.
The -l and -p options specify the size of the new pane in lines (for vertical split) or in cells (for horizontal split), or as a percentage, respectively. The -b option causes the new pane to be created to the left of or above target-pane. All other options have the same meaning as for the new-window command.

you can bind the command also, e.g.

bind '"' split-window -v -c "$PWD"  # Split panes horizontal
  • Thanks but none of the suggestions work: what I want is for the new session to open with the pwd that I'm in in the other pane. The first one always moves to a hard-coded dir and the second suggestion always brings me to ~, not to whichever dir I'm currently in in the previous pane. – Juan Alonso May 27 '16 at 7:53
0

You're running into two problems:

  1. tmux tries to get the current working directory from the current process group (returned by tcgetpgrp(3) or the TIOCGPGRP ioctl), so variables such as #{pane_current_path} will always reflect temporary directory changes in the currently-running command.

  2. tmux version 1.8 doesn't expand any #{variables} in the -c argument to split-window. Trying to use any expansion there opens the new pane at /. This changed between 1.8 and 1.9.

You can work around the first by instead using the #{pane_title} variable (alias #T), because that can be updated on-demand with an escape sequence:

When a pane is first created, its title is the hostname. A pane's title can be set via the OSC title setting sequence, for example:

$ printf '\033]2;My Title\033\\'

You can use a custom PS1 to send this sequence to tmux (you may have seen a similar trick used for changing the xterm title):

if [ -n "$TMUX" ] ; then
  PS1='\[\e]2;$PWD\e\\\]'$PS1
fi

This will respond to cd (or pushd etc.) in the interactive bash, but not to other foreground processes - including any bash aliases, provided that they don't display a prompt.

Then change the keybinding in your tmux.conf to:

bind-key '"' split-window -c "#T"

But you'll probably have to update to a newer tmux version.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.