DEV Community

Yutaka HARA
Yutaka HARA

Posted on • Updated on

Automatically prepare windows at tmux startup

Do you have a rule for tmux windows? For example, on which window do you start your editor? Here is my rule.

  • Window 0: reposh
  • Window 1: vim
  • Window 2: zsh
  • Window 3: Server process (i.e. rails server)

It means that everytime I start tmux, I open 4 windows and run these. Today I learned to do it automatically.

Command file

The first step is to write a tmux command file. This is a list of tmux commands that I want to run:

$ cat ~/proj/dotfiles/tools/tmux_multi
new-window
new-window
new-window
select-window -t 0
send-keys 'reposh' Enter
select-window -t 1
send-keys 'vim' Enter

Then you can load this file at tmux startup by:

$ tmux new-session ";" source-file ~/proj/dotfiles/tools/tmux_multi

Alias

Of course this is too long to remember. So I added an alias to my .zshrc:

alias tmux!='tmux new-session ";" source-file ~/proj/dotfiles/tools/tmux_multi'

Now my tmux sets up the 4 windows by tmux!.

Tips: setting iTerm2 tab title

If you are using iTerm2 on Mac, it is useful to set the tab title before starting tmux.

alias tmux!='TAB_NAME=`ruby -e "print Dir.pwd.split(\"/\").last"`; echo -ne "\e]1;$TAB_NAME\a"; tmux new-session ";" source-file ~/proj/dotfiles/tools/tmux_multi'

Top comments (1)

Collapse
 
micahriggan profile image
Micah Riggan

Never knew how to do this! I often have different tmux configurations for different projects, I eventually found github.com/tmux-python/tmuxp which uses a yaml file to describe the windows, but it's cool to know how to do this without using another tool.