Ruby
Article

10 Killer Tmux Tips

By Benjamin Tan Wei Hao

tmux3

In this article, I bring you ten of my favorite tmux tips. For those unfamiliar with tmux, SitePoint Ruby Editor Glenn Goodrich has written an excellent overview of the basics.

Today, I am going to show you the tips and tricks I use with tmux on a daily basis, as well as how tmux can improve the workflow of your Ruby development. If you are a vim user, you will be in for a treat.

Tip #1: Always Have a tmux Session On

Getting into a new habit is always an uphill battle. Just like trying to force myself into using the hjkl keys in vim by disabling the arrow keys, I needed to find a way to force myself to use tmux all the time. This is useful when you are in the middle of coding and you forgot that you should have tmux-ed in the first place.

The above example works with iTerm2 on Mac OSX. It shouldn’t be difficult to search for Linux equivalents. The main idea is to always run tmux attach -t base || tmux new -s base when the terminal program starts up.

The line above attempts to connect to a previously created session called base, or creates a new one (also called base) if it doesn’t exist. You can rename base to anything you want, it is not a special keyword. With this incantation, whenever you fire up the terminal, you will automatically connect back to your previous tmux session.

Tip #2: Multiple Pane Synchronization

The video above shows four panes loaded with REPLs of four different languages. Going in a clockwise direction, we have Haskell, Ruby, Elixir, and Python. What setw synchronise-panes gives us is that it simultaneously sends keyboard input from one pane to the rest of the panes. (In this case, we can also see the the Elixir REPL is far superior because of the colored output.)

Obviously, this demonstration is a mere party trick to impress developers and nothing more. A good use case is having to perform a repetitive operation on multiple machines. For example, imagine having a tmux session connected to four remote hosts and having to executer the exact same command on all of them.

Tip #3: Refreshing tmux

I am prone to clearing the terminal with Command + L. Unfortunately in tmux, this just messes up the entire session. To illustrate, the above shows my typical tmux experience: I am happily programming when somehow, my fat fingers hit Command+k and my entire tmux session gets messed up, making me mad:

Thankfully, the easy fix is to bind prefix-r to reload the tmux configuration file, and everything is nice again. In ~/.tmux.conf, add the following entry:


bind-key r source-file ~/.tmux.conf

This tells tmux to source the configuration again, essentially reloading tmux.

Tip #4: The tmux Plugin Manager

This is a recent discovery of mine. I didn’t realize someone had created a tmux plugin manager. I won’t bore you with the details of installation, since it is pretty straightforward.

Once you get the plugin installed, it’s only a matter of selecting the plugin you want and pasting the entry into tmux.conf.

If you use Vundle or Pathogen for vim, the process is the same. You declare whatever plugins you want by supplying the GitHub username and repo, save tmux.conf, then hit prefix + I to install the plugin:

The next two tips cover two interesting plugins that can be installed with the tmux plugin manager.

Tip #5: Resurrecting tmux

tmux-resurrect is a tool to persist a tmux environment across system restarts. Why is this useful? When you restart tmux, your entire tmux session is gone. tmux-resurrect fixes that. Another very nice feature of tmux-resurrect is the ability to restore vim sessions too!

In the video that follows, I have a vim session in the left pane rails console and rails server, respectively, on the right.

In order for tmux-resurrect to persist a vim session, install Tim Pope’s vim-obsession. vim-obssesion makes it easier to record a vim session. I haven’t played with it much, but let me show you the entire process:

First of all, we have to tell vim-obsession to track the session. I also have htop running in another window.

In order to save an entire tmux session, type prefix + Control + s. Then if something kills the tmux server, which is what happens when your battery runs out or when someone trips over the wire and the power gets cut. With tmux-resurrect, losing your tmux session is not an issue. In order to restore the session, you need to run tmux again, and this time, hit prefix + Control + r.

However, I can already here some people thinking, “Isn’t manually doing prefix + Control + s + r is a hassle! Who has time to remember all that?” Enter tip #6.

Tip #6: tmux-continuum

tmux-continuum continuously saves your tmux environment at regular intervals and automatically restores it when tmux is started.

Tip #7: Zooming tmux

Zoom was introduced in tmux 1.8. tmux zooming is very useful when you want to look at test failures or inspect logs. To zoom into a pane, hit prefix + z, and use the same combination to zoom out.

Tip #8: Navigate Seamlessly Between vim Splits and tmux Panes

It can sometimes be confusing to figure out whether you are in a vim split or in a tmux pane. Navigating between both is also a hassle. That is until vim-tmux-navigator came along.

Here, we have a tmux session with three panes. The left half consists of two vim splits, and the right hand side contains two tmux panes.

With vim-tmux-navigator, I can navigate across vim splits and tmux panes using Control + hjkl:

Tip #9: vim + tmux + Ruby Specs

vim and tmux make a very nice combination to run Ruby tests. For this to work, you need to install three vim plugins:

Once you have these plugins installed, let’s see how they make our workflow better:

We have the specs for a Micropost model. I have configured to runs all the specs found in this file. However, when I hit , it runs only the specs under the cursor. Extremely handy!

Tip #10: tmuxinator

In addition to having an awesome name, tmuxinator is an excellent Ruby gem that helps you create an manage tmux sessions easily. tmuxinator is especially useful when you have different projects requiring different layouts of panes/windows.

Thanks for Reading!

I hope you enjoyed this article, and learned something new. Do you have other tips to share? Spread some tmux love in the comments below!

Resources

More:
  • Kyle Ries

    Great article Benjamin! Perhaps you have a localized version running, but for others, the option to synchronize panes is “synchronize-panes” with a “z”, source: http://sourceforge.net/p/tmux/tmux-code/ci/master/tree/examples/tmux.vim#l252. Quite a subtle difference, but I just wanted to drop a note for anyone that overlooks it (as I did)!

  • Ilia Lobsanov

    You can also do `prefix + q` to redraw the tmux panes after an accidental command+K

  • http://www.codedtrue.com Dane Balia

    Great article thanks Benjamin! The eventual thing that put me off tmux despite enjoying my experience in it, is the “effort” needed to copy and paste large subset of lines and how that in split screen it would copy the next screens text as well. Do you have a cure for that poison?

    • http://benjamintan.io/blog Benjamin Tan Wei Hao

      Assuming you have vi style bindings ….

      1) enter copy mode using prefix + [
      2) navigate to beginning of text, you want to select and hit Space
      3) move around using arrow keys to select region
      4) when you reach end of region simply hit Enter to copy the region
      5) now prefix + ] will paste the selection

      That’s how I do it. Unfortunately, I totally agree it’s a hassle but once you get used to it, you’ll be fine.

      Another way could be zooming into the pane first. On my setup, it’s prefix + z.

      (Source: https://awhan.wordpress.com/2010/06/20/copy-paste-in-tmux/)

    • http://buildingrails.com/ Adam21e

      Yeah, I hated that as well. But there is a fix! Tip #7 – Zooming – if you zoom in the pane it goes to full screen and then you can mouse highlight to copy without getting anything else in there.

  • Vadim

    With #3 you’re killing symptoms not the issue itself.

  • http://benjamintan.io/blog Benjamin Tan Wei Hao

    Many thanks!

  • Matt Lambie

    The zoom is a killer tip!

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Ruby, once a week, for free.