Migrating from GNU Screen to Tmux

This post was originally published on the New Bamboo blog, before New Bamboo joined thoughtbot in London.

A while back I took the plunge and moved from using GNU Screen to Tmux for all of my terminal multiplexing needs.

I’d been a happy Screen user for quite a few years. I’m also a Vim user and this combination means I get to spend a lot of time in the terminal. There were a few reasons I finally decided to make the switch. The biggest one was that here at New Bamboo I was the sole Screen user surrounded by many enthusiastic Tmux users (including the author of Hermes) and I felt I was missing out on being able to tap into all this expertise!

Almost as importantly, Tmux’s great UTF-8 support means I can include an adorable panda face in my prompt:

Easing the transition

There were a few specific tips and bits of configuration which made the initial transition from Screen very pain free and allowed me to get up and running quickly:

Starting a new session

I was used to starting or reattaching to a Screen session with:

$ screen -UdR

The flags tell screen to use UTF-8 encoding (U), detach elsewhere and reattach here (d) and finally to reattach if possible, otherwise start a new session (R).

Tmux requires us to be a little more explicit. To start a new session:

$ tmux new -s <session name>

To reattach to an existing session:

$ tmux attach -t <session name>

That’s a little too much typing for my liking, so I alias these two commands to tn and ta respectively.

To detach (as in Screen):

<prefix e.g. C-a> + d

Tmux automatically turns on UTF-8 support if it successfully detects a UTF-8 capable terminal, so there’s usually no need to specify this explicitly.

Remapping prefix to Ctrl-a

By default Tmux commands are prefixed with Ctrl-b. In Screen it’s Ctrl-a, which has been firmly committed to my muscle memory. Fortunately it’s easy to change this by adding the following to our tmux.conf:

unbind C-b
set -g prefix C-a

Toggling windows with Ctrl-a + Ctrl-a

Great, now we can move around our windows with Ctrl-a + n, Ctrl-a + p and Ctrl-a + <window-number> just like in Screen.

One missing piece is the ability to toggle back to your previous window with Ctrl-a + Ctrl-a. This can be achieved by adding the following to our tmux.conf:

bind-key C-a last-window

Jump to beginning of line in Bash

In Bash (using the default readline key bindings) Ctrl-a moves the cursor to the beginning of the line. Within Screen this becomes Ctrl-a + a. To enable the same combination within Bash in a Tmux window add the following to your tmux.conf:

bind a send-prefix

Vim Copy/Paste under Mac OS X

As a Vim user, I noticed pretty quickly that Vim/OS X clipboard integration (enabled in my vim config with set clipboard=unnamed) didn’t work under Tmux. A quick Google led me to Chris Johnsen’s excellent set of notes and workarounds which provides a wrapper script you can use to start all new Tmux windows. This fixes clipboard integration and got me back the functionality I was missing.

Accessing scrollback history

I was happy to find that paging/searching back through terminal scrollback history is exactly the same in Tmux as in Screen. To enable copy mode use Ctrl-a + [. From here you can page up and down with Ctrl-u and Ctrl-d. You can also search backwards using ? followed by your pattern. To exit copy mode use Ctrl-c.

Final thoughts

I’ve been using Tmux for a while now and I’m really happy with it as part of my toolchain. There’s a great community out there so it’s generally straightforward to find answers to questions and solve any issues. It’s also under active development with regular releases which is great news for users.