More tmux scripting

Following on from Saving time with a preset tmux setup I recently created a more convoluted indepth setup.

Link to this section The Script

setup.sh:

#!/bin/zsh

# Note that this assumes base index of 1

CWD=$(pwd)
SESSION_NAME="$1"

# detach from a tmux session if in one
tmux detach > /dev/null

# Create a new session, -d means detached itself
set -- $(stty size) # $1 = rows $2 = columns
tmux new-session -d -s $SESSION_NAME -x "$2" -y "$(($1 - 1))"

tmux new-window -t $SESSION_NAME:1 -n 'code'
tmux new-window -t $SESSION_NAME:2 -n 'test'
tmux new-window -t $SESSION_NAME:3 -n 'php'
tmux new-window -t $SESSION_NAME:4 -n 'vim'
tmux new-window -t $SESSION_NAME:5 -n 'falcon'
tmux new-window -t $SESSION_NAME:5 -n 'task'

## Test Window
tmux select-window -t $SESSION_NAME:2
tmux split-window -h -l70
tmux split-window -v -l50

tmux select-pane -t 1
tmux send-keys "~pct" C-m
tmux send-keys "v" C-m
tmux select-pane -t 2
tmux send-keys "~pct" C-m
tmux send-keys "/usr/local/bin/chromedriver --url-base=/wd/hub" C-m
tmux select-pane -t 3
tmux send-keys "~pct" C-m

## PHP Window
tmux select-window -t $SESSION_NAME:3
tmux split-window -h -l70

tmux select-pane -t 1
tmux send-keys "~pcp" C-m
tmux send-keys "vl ep" C-m
tmux select-pane -t 2
tmux send-keys "~pcp" C-m
tmux send-keys "php -a" C-m

## Vim Window
tmux select-window -t $SESSION_NAME:4
tmux send-keys "cd ~/.config" C-m

## Falcon Window
tmux select-window -t $SESSION_NAME:5
tmux split-window -h -p50

tmux select-pane -t 1
tmux send-keys "~falcon" C-m
tmux select-pane -t 2
tmux send-keys "~falcon" C-m

## Taskwarrior
tmux select-window -t $SESSION_NAME:6
tmux send-keys "~/.config" C-m
tmux send-keys "t" C-m

## Main Window
tmux select-window -t $SESSION_NAME:1
tmux rename-window 'code'
tmux split-window -h -l70
tmux split-window -v -l50

tmux select-pane -t 2
tmux send-keys "~pcp" C-m
tmux select-pane -t 3
tmux send-keys "~pcp" C-m
tmux send-keys "figlet -f roman Ready! | lolcat -t" C-m
tmux select-pane -t 1
tmux send-keys "~pcp" C-m
tmux send-keys "v" C-m

# Finally attach to it
tmux attach -t $SESSION_NAME

Now this is mostly standard fare (covered in the linked post) but I think there are a few additions worth jumping into.

Link to this section Directory hashes

Zsh has a wonderful feature called a directory hash which looks like the following:

hash -d dir=~/dir

This will then allow you to put ~dir in the place of any shell command where a directory could be used and also you just type in ~dir to cd into it.

Link to this section Triggering a leader command on Vim startup

It’s quite usual to have a number of common config files editable via some Vim mappings. e.g. <leader>ev to edit the Vim config, <leader>et to edit the tmux config etc. But what if you wanted to directly trigger such a mapping on Vim startup?

One way to achieve this is with the following in your .zshrc:

run_vim_leader() {
  com="NormLead $1"
  nvim -c "$com"
}

alias vl='run_vim_leader'

…paired with the following vimscript in my .vimrc:

function! ExecuteLeader(suffix)
  let l:leader = get(g:,"mapleader","\\")
  if l:leader == ' '
    let l:leader = '1' . l:leader
  endif
  execute "normal ".l:leader.a:suffix
endfunction
command! -nargs=1 NormLead call ExecuteLeader(<f-args>)

nnoremap <leader>ev :edit $MYVIMRC<cr>

Now you can simply do vl ev to edit your .vimrc from your shell. At this point, you may be saying to yourself ahem, I could just make an alias in …but then how would you use that when you are in Vim itself? and what if you want to run other leader commands on startup? huh?! tenuous / contrived? I think not.

Link to this section Starting up slime and a PHP REPL

Having a local REPL to try out random snippets is a great time saver. An easy enough way to do this with Vim is using the Slime plugin. In my case I setup a tmux split on the right running php in interactive mode (php -a) which I can send test code to from Vim with just a (either from visual mode or of the current paragraph). To get Vim to startup ready to go I have the following in my .vimrc:

nnoremap <leader>ep :call StartSlime()<cr>

function! StartSlime()
  execute 'bd'
  let g:startify_disable_at_vimenter=1
  execute 'new'
  execute 'only'
  execute 'set ft=php'
  call append(0, "<?php")
endfunction

Essentially this will:

  • destroy the Startify buffer
  • make a new buffer
  • make it the only buffer
  • set the filetype to php
  • append <?php to the top of the buffer

I then start this up via the run_vim_leader bash function via vl ep (see above).

Link to this section The complete PHP window

Just to bring that all together here is the complete PHP window part of the tmux setup script:

tmux select-window -t $SESSION_NAME:3
tmux split-window -h -l70

tmux select-pane -t 1
tmux send-keys "~pcp" C-m
tmux send-keys "vl ep" C-m
tmux select-pane -t 2
tmux send-keys "~pcp" C-m
tmux send-keys "php -a" C-m

…sweet!