Creating tmux layout with ZSH scripts

806 views Asked by At

Im trying to script up a nice tmux config but I'm having some problems with how to target panes for splitting and selections. I want to create the following layout where the Vim pane is selected as default. I also want to create a second window as is seen in the script but this is not the problem.

---------------------
|        VIM        |
|-------------------|
|         |         |
|  ZSH    | ghci    |
|-------------------|


session=$1
tmux has-session -t $session
if [ $? != 0 ]
then
     tmux new-session -s $session -n editor -d
     tmux send-keys -t $session 'vim' C-m
     tmux split-window -v -p 20 -t $session
     tmux split-window -h -t $session:2
     tmux new-window -n console -t $session
     tmux select-window -t $session.1
     #tmux select-pane -t $session:1.1
     tmux attach -t $session
else
     echo 'SESSION ALREADY EXISTS'
fi

So my questions are:

How how can I target a specific pane to split horizontally so that I can create the ZSH/ghci split? How do I target a specific pane to execute a command such as ghci?

1

There are 1 answers

0
chepner On

The -t option to split-window can take an argument of the form <sessionname>:<windownumber>.<panenumber> which you can use to specify exactly which pane should be split. new-session can take an argument specifying which command to run in the initial window, which removes the need for send-keys. A similar argument is used with split-window to specify what command to run in the new pane if not the default shell.

session=$1
tmux has-session -t $session
if [ $? != 0 ]
then
     tmux new-session -s $session -n editor -d vim
     tmux split-window -t "$session:0.0" -v -p 20 
     tmux split-window -t "$session:0.1" -h ghci
     tmux attach -t $session
else
     echo 'SESSION ALREADY EXISTS'
fi

Of course, the pane closes when its command ends, so you want to be able to close vim/ghci without destroying the pane, stick with send-keys (whose -t argument can take the same pane identifier used with new-session and split-window), or look at the remain-on-exit option.