DEV Community

Cason Adams
Cason Adams

Posted on • Updated on

Pair Programing with vi and tmux

Working remotely is really the only way to do things moving forward. However, it is more difficult to pair program when you are not sitting side by side at the office. There are some nice tools out there that make this remote pair programing easier. But they are GUIs and slow and I want speed! So here is what I have done to pair program effectively.

Requirements

  1. tmux
  2. vi or terminal based editor
  3. ssh enabled box

Setup

First only pair program with those you trust to be on your machine.

Add users to ~/.ssh/authorized_keys

command="tmux attach -t pair -r" public_ssh_key1
command="tmux attach -t pair -r" public_ssh_key2
command="tmux attach -t pair -r" public_ssh_key3
Enter fullscreen mode Exit fullscreen mode
  • Note you might need to provide the full path to tmux here.

Add tmux read only toggle to ~/.tmux.conf

bind-key R switch-client -r
Enter fullscreen mode Exit fullscreen mode

Set window size so windows don't bounce in ~/.tmux.conf

set-option -g window-size smallest
Enter fullscreen mode Exit fullscreen mode

Port forward port 22

Since we are going to use vanilla ssh to pair program we need to port forward port 22 to the machine you are going to pair on.

In my case I setup a dynamic DNS using duckdns

sshd_config

We need to tell sshd to use ssh keys instead of passwords
Add or update the following in /etc/sshd_config

PasswordAuthentication no
ChallengeResponseAuthentication no
Enter fullscreen mode Exit fullscreen mode

Setup tmux pair session

Now that things are ready to go we made it so connections can only connect to tmux sessions labeled pair.

This requires that tmux new -t pair be created before anyone can connect.

So host computer will setup the tmux new -t pair and the client(s) would then ssh user@ddns.org Once a peer has connected they connect in readonly mode. To toggle edit mode just ctrl+b R. And again to go back to readonly mode.

There are many different ways to set this up and get running but this is the basic idea. I have been pair programming with way for quite some time and it is very fast and low latency.

Exit the session as a client with ctrl+b d. If the host closes the session all peers will be disconnected.

Closing notes

With tmux 3.2 or greater you can setup multipane edits. As an example one can split panes ctrl+b % and when a peer connects the host can work in the pane on the left and the peer can work in the pane on the right etc.

Simply modify the command in the ~/.ssh/authorized_keys file to look like this.

~/.ssh/authorized_keys

command="tmux attach -t pair -r -factive-pane" public_ssh_key1
command="tmux attach -t pair -r -factive-pane" public_ssh_key2
command="tmux attach -t pair -r -factive-pane" public_ssh_key3
Enter fullscreen mode Exit fullscreen mode

Add on fun

Have a quick read on my post about tmux-dimming to enhance the pair programming experience.

Top comments (4)

Collapse
 
gayanhewa profile image
Gayan Hewa

Interesting. I had good experience remote pairing using Live share on vscode.

Collapse
 
casonadams profile image
Cason Adams • Edited

It isn't that it is a bad experience. Teletype for Atom (uses webRTC) is pretty slick too. It is that my preferred editor is vi. There are other solutions like tmate also. This example is more or less a way to do it. And my preferred method.

Collapse
 
gayanhewa profile image
Gayan Hewa

True. It's good know of other ways to do it 🙇

Collapse
 
samf profile image
Sam Falkner

I was wondering if you were going to mention the ForceCommand directive in sshd_config, but then I saw how you did it via authorized_keys. I had no idea you could do that! Very nice.