I used iTerm2. I often split the pane into two windows. When I want to change the directory in one, I want to also change the directory in the other automatically. Is this possible? A Tmux solution would also be welcome.
-
In my experience each terminal is its own session. Any way of calling the `cd` command in one terminal can't influence your working directory on the other. – ProdIssue Feb 18 '17 at 19:05
-
What if the 2nd window is running a program, say vi? Or if the 2nd window has a partial command entered? – Jeff Schaller Feb 19 '17 at 02:59
2 Answers
I haven't yet used tmux, so there might be better ways to do this. This answer is also bash-centric, since that's the shell I'm most familiar with. I'm sure other shells have similar concepts that this could be adapted to.
The basic idea is to have the initial window source a script that sets up a function that's called just before a new shell prompt is displayed; the second window would source a separate, second script. The first script's function updates a TMUX_PWD tmux variable whenever the current directory changes. The second script's function checks that TMUX_PWD variable and changes directory if its current directory is different.
The code for the primary window:
function prompt_command_primary {
if [[ "$(tmux showenv TMUX_PWD | cut -d= -f2)" != "$PWD" ]]
then
tmux setenv TMUX_PWD "$PWD"
fi
}
PROMPT_COMMAND='prompt_command_primary'
# initialize it
tmux setenv TMUX_PWD "$PWD"
The code for the secondary window:
function prompt_command_mirror {
if [[ $(tmux showenv TMUX_PWD | cut -d= -f2) != "$PWD" ]]
then
cd -- "$(tmux showenv TMUX_PWD | cut -d= -f2)"
fi
}
PROMPT_COMMAND='prompt_command_mirror'
# initialize ourselves
prompt_command_mirror
This seemed to me to be the most graceful way to have the second window change directory based on the first window.
- 66,199
- 35
- 114
- 250
Iterm2
broadcast to all panes in tab
command+option+i
broadcast to all panes in all tabs
command+shift+i
Note
Toggle on / off using the same key combo.
From Menu
Shell > Broadcast Input
- 206
- 1
- 5