Skip to content

Instantly share code, notes, and snippets.

@biscuitvile
Last active February 9, 2018 00:06
Show Gist options
  • Save biscuitvile/90957887e56b83d9a2c6 to your computer and use it in GitHub Desktop.
Save biscuitvile/90957887e56b83d9a2c6 to your computer and use it in GitHub Desktop.
Improved Remote Pairing with Vim, Tmux and Tmate (IMO)

Improved Remote Pairing with Vim, Tmux and Tmate

Goals

  • Spin up tmux sessions for each app automatically
  • Have vim started in each tmux session
  • Have a console successfully started for Rails apps, regardless of Rails version
  • Tail the pow logs for Rails apps
  • Start Ember server and test runner for Ember apps
  • Maintain vim sessions automatically so vim is always "as we left it"
  • Easily start a tmate session running an existing tmux session for easier pairing
  • Fix the tslime issue with "tmux send-keys" not working under tmate for running tests from vim while pairing

Overview

A combination of shell scripts and config files from the following repos will acheive these goals:

https://github.com/biscuitvile/bin

https://github.com/biscuitvile/dotfiles

You will basically want everything from the linked bin repo and want it available in your PATH. I have this bin directory in my home directory but obviously you may have a bin directory of your own there.

  • The setup_session script sets up individual tmux sessions addressing the goals listed above
  • The bcmux script sets up all tmux sessions for BC apps we work on
  • The mate script opens tmate running a provided tmux session and copies the tmate ssh command to the clipboad

An example from a rebooted machine running no tmux server looks like this:

andy@lolz:~% bcmux
Setting up session admin.
Setting up session admin-frontend.
Setting up session bigcartel.
Setting up session checkout.
Setting up session hub.
Done!
andy@lolz:~% mate admin

The last command opens the admin tmux session nested inside tmate.

Tmate configuration

Nesting tmux inside tmate requires some adjustments to make any sense. You'll want a ~/.tmate.conf similar to the one in my dotfiles repo:

# set a tmate prefix
set -g prefix C-]

# set color of status line
set -g status-fg white
set -g status-bg black

# eliminate left and right status bars
set -g status-left-length 0
set -g status-right-length 0

This sets a tmate-specific prefix binding that (albeit a little awkward) you really don't even want to be using much at all. If you're running tmux inside tmate and opening a lot of windows and panes in tmate you're... doing it confusingly. Without this remapping you'd have to use your tmux binding twice in a row to send commands to tmux. Not desirable at all.

The adjustments of the status bar are purley aesthetic but keep you from experiencing a weird 'double status bar' effect from the nesting. These changes make the status bar just helpful enough to see that you've nested tmux and tmate, but keeps tmate low key in appearance. Again in my opinion you don't really want to use tmate for any splits or windows, just as a vessel to facilitate remote pairing. Chances are your status bar will just show "0:tmux*" all the time against a black background.

Vim configuration

In order to restart vim where you left off for each project all the while killing and re-establishing tmux and tmate sessions (or even across hard reboots of your machine), vim sessions are taken advantage of in the following lines of the setup_session shell script:

# if a vim session for project already exists
# open vim from the session, otherwise open
# vim in project directory and write session

vim_session="$HOME/.vim/sessions/$session.vim"

if [ -f "$vim_session" ]; then
tmux send-keys 'vim -S ' $vim_session 'Enter'
else
tmux send-keys 'vim .' 'Enter'
tmux send-keys ':mksession ' $vim_session 'Enter'
fi

This is good but not 100% effective as you'd need to manually write your vim sessions to maintain them. In order to avoid this the following lines from my linked .vimrc.local file writes vim sessions automatically when saving files:

" Re-write sessions automatically on save
autocmd BufWrite * call RewriteSession()

function! RewriteSession()
  if !empty(v:this_session)
    exe "mksession! " . v:this_session
  endif
endfunction

Result

The end result is a mostly automatically maintainable work environment for tmux, configured for easy remote pairing with tmate. When you're deep in the middle of working in a tmux session and want to pair with someone, you don't have to expend much effort to start pairing by rebuilding the state of your tmux session in tmate first. Running tests from vim with turbux and jumping into other tmux sessions with your pair tagging along Just Works. If you're working in a long running tmate session alone and want to pair but the ssh token has expired, spinning up a new tmate exactly where you left off and having the updated ssh command added to your clipboard is a matter a few keystrokes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment