Developer Aspirations

YAPB - Yet Another Programming Blog



April 2014

Upgrade Your Command-Line - Part 3: tmux

by Colin Miller, on command-line, bash, tmux

On to part 3 of my 5 part series on upgrading your command-line. In part one I talked about improving the speed of navigating your filesystem using bashmarks. In part two, I expanded on that navigation speed by improving the cd .. command with an improved version. In this part, I'd like to talk about multi-tasking in the terminal itself.

Many Unix veterians are familiar with screen which allows you to use virtual terminals. An extra-useful version of this is called tmux and it can accelerate your command-line effectiveness. A small set of the features of tmux is the ability to have virtual tabbed consoles in a single terminal window, split screen virtual terminals, and disconnect and reconnecting to a terminal from a remote location.

Installing tmux is as easy as brew install tmux (in OS X with homebrew) or apt-get install tmux in a debian-based Linux distribution. Other distributions most likely also have installers via their package managers or you can just download it from the tmux site. After that, just run tmux to start up your virtual terminal.

Tmux uses a special keyboard combination to send it commands. It is called the bind-key and by default it is set to ctrl-b. You can change this in the ~/.tmux.conf preference file however (I set mine to ctrl-a as is the default in screen). For a list of commands you can use in a tmux session, use ctrl-b ?. That's ctrl-b, then seperately hitting the ? key after you have let go of ctrl-b.

Useful Commands

Here's some useful commands to try out. Remember that bind-key is a standin for ctrl-b or whatever you have set your bind-key to.

  • bind-key " - splits pane horizontally
  • bind-key % - splits pane vertically
  • bind-key <arrow key> - switch working pane
  • bind-key c - create a new window
  • bind-key <number> - switch to window of the specified number
  • bind-key x - force kill the current pane
  • bind-key d - detach tmux and leave it running in the background.

You can reattach to a detached tmux session using starting tmux with tmux attach. Split panes make it easy to have multiple windows open on the same screen to view one set of data while working with another. Multiple windows are useful if you have a long-running command going on but want to do something else while it runs. This is also good if you want to leave an editor open in one window while running commands or a different editor in another window.

There are a load of additional commands and customizations one can do to tmux. Personally I remapped splitting panes to be bind-key | and bind-key - for vertical and horizontal respectively. I also rebound the pane movement keys to use h j k and l rather than the arrow keys as I'm a heavy vim user.

The largest benefit from usings tmux rather than just multiple terminal windows is that you never need to use the mouse. You can keep your hand on the keyboard and navigate without using a mouse which can greatly speed things up. There is also a built-in back-scroll and copy and paste set of commands that can make things even easier.

For more information on using tmux you can search for a variety of tutorials. The Pragmatic Programmers have also written a book on the subject for a more in-depth look.

Next up: part 4: better prompt

comments powered by Disqus