Primary image for tmux: Stay focused

tmux: Stay focused

On the surface tmux manages your terminal windows, but really it helps you stay focused. The challenge with learning tmux is that the benefits are not immediately apparent. At first it’s just screen with vertical splits. In this post I hope to convince you to use it long enough to benefit.

Aside from focus, the second thing tmux will give you is better Unix skills. Tools, libraries and programming languages all change, but Unix stays. Improving your Unix skills is one of the best long term knowledge investments you can make.

First install it - it’s in your Linux package manager and in OSX brew. Next create .tmux.conf in your home directory, and add these lines:

set -g default-terminal "screen-256color"
set -g repeat-time 125
set -g base-index 1     # count from 1

# Change prefix key to backtick (`)
unbind C-b
set-option -g prefix `

# So we can still use ` when needed
bind-key C-a set-option -g prefix C-a
bind-key C-b set-option -g prefix `

# Easy bindings for split
unbind %
bind | split-window -h
bind - split-window -v

Good key bindings make all the difference in tmux, and the defaults are finger breaking. If on your keyboard backtick ` is not top left, next to 1, then you may want to change the set-option -g prefix line.

Vertical split for focus

Save your config, and start tmux. Open your text editor of choice. Write a short program. Hey, let’s run it check it works. Instead of opening a new terminal window and Alt-Tab-ing your way to distraction, hit ` then | (backtick then pipe). Your screen should split vertically, and your cursor is in the new pane. Run your program!

tmux vertical split

To move between panes, use ` then the arrow keys. Next, say you need to try something out in ipython. Switch to the pane on the right, press ` then - for a horizontal split. Once you’re done with a pane Ctrl-d closes it (Ctrl-d stops / closes most anything in Unix). Scrollback in a pane is ` PgUp and you just hit Enter to exit scrollback mode.

tmux vertical and horizontal split]

If you need a whole new window, instead of splitting the current one into panes, press ` c (for create). Move between your windows with ` 1 or ` 2 etc. On my keyboard ` is next to the number key row, which is why I use it as the tmux prefix. If you need a literal `, press ` Ctrl-a to switch the prefix to Ctrl-a. Press Ctrl-a Ctrl-b to switch back. Many people use Ctrl-a all the time (it’s the screen default), but I use the tmux prefix many times more often than a backtick.

A nice side effect of living in tmux is that [c]reate is how I get a new window - on remote servers, on OSX, in Gnome, KDE, etc. tmux and a browser, that’s all I ask for.

Tail all the logs

Another handy example of panes is monitoring log files. Say you want to monitor three log files at once to track an error, or log files on three separate servers. Press ` - (backtick then dash) twice, which should give you three panes vertical panes. Now press ` several times, until you find an arrangement you like.

tmux triple split

Detach, pair program

Press ` d (for detach) to disconnect from tmux. Type tmux attach to re-connect. In the afternoon, if I get sleepy working at my desktop, I switch to a laptop at a standing desk, or a coffee shop. I ssh in to my desktop from my laptop, tmux attach, and everything is exactly as I left it.

With the right permissions, other people can attach to that tmux, and pair program with you. We’ll sometimes leave a tmux session on one of our servers and pair in there.

Commands, simple scripting

In a window with more than one pane, press ` and : (backtick then colon). You should see a bar open at the bottom of your screen. Type break-pane (you can Tab complete). You pane will break out to a new window. Lets join that pane back up. Switch back to the first window, press ` : again and type join-pane -s 2 where 2 is the number of the new window in the bar at the bottom of your screen. The -s stands for source, the source window to join into the current one as a pane.

Now let’s do that again, but scripting. In a pane type tmux break-pane. Type that at the normal unix command line. Switch back to the window you just ‘broke’ from, and type tmux join-pane -s 2. Any tmux command can be given at the command line, hence can be put in a script. Example tmux script are easy to find, StackOverflow tmux tag has many good example.

Config, status bar

tmux status bar

Along the bottom of tmux is the status bar. From left to right I have:

  • Current user and host
  • Window list, with window numbers (for ` switching) and the active program
  • The next entry in my calender
  • Remaining laptop battery time
  • User and machine again (can’t be too careful)
  • Load average
  • Date and time

You can put basically anything in there. It’s configured in .tmux.conf.

Stick with it

tmux, like vim or emacs is a journey, and one well worth taking the first steps on. By now you already know the core commands, and you’ll learn more over time. Ninety percent of my tmux usage is splitting a window into panes. If I don’t take my eyes off my editor, I can’t lose focus.

Graham King

About the author

Graham King

Graham's life has revolved around growing software since age 12. After many years of C, and more of Java, Graham fell in love with Python in 2003 and Django in 2005. Most of his previous career …

Graham is no longer active with Lincoln Loop, but you can find him at https://www.darkcoding.net.