Book Image

Getting Started with tmux

By : Victor Quinn
Book Image

Getting Started with tmux

By: Victor Quinn

Overview of this book

Table of Contents (16 chapters)
Getting Started with tmux
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Why tmux?


Many developers spend much of their day in a terminal. Whether using it for coding; SSH sessions to remote servers; browsing the filesystem; local tasks such as checking, compiling, or linting code, running unit tests; or even for mail or Internet Relay Chat (IRC), the terminal is one of the most widely used weapons in the developer's arsenal.

tmux is a command-line application that runs within your terminal and turbocharges it. Its powerful features allow for the simplification of many everyday tasks, as illustrated throughout this book. However, the main reason most people use a program like tmux is that it allows you to take a single terminal window and turn it into many virtual windows, each having their own state. It is one in a class of applications called terminal multiplexers (tmux is just a shortening of this term). It has some brethren, but the most prominent is GNU Screen.

Remember browsing in web browsers without tabs? If you answered no, you weren't missing much, and enjoy your youth! However, if you answered yes, it might take you a second to remember how much less convenient it was. Sure, there was nothing you can do with multiple tabs that you couldn't with no tabs and many browser windows, but it was still a very helpful boost to productivity to be able to group them together. Having many browser windows got very confusing. The advent of tabs allowed you to separate your browsing experience logically. Before tabs you would have had one browser window for reference documents, another for shopping, and so on.

Likewise, a terminal multiplexer such as tmux allows you to do the same, logically grouping multiple windows into a single terminal window, like tabs in a browser. Just as each browser tab contains its own state, so does each tmux window.

However, unlike browser tabs, which mostly just add organization, tmux actually adds a rich set of additional functionality to the terminal. Not only does tmux allow for dividing a window into panes so that multiple different bits of content can be on the screen at the same time, but tmux also adds Copy mode and other constructs that allow heavy terminal users to do more without leaving tmux or even having to reach for their mouse as often. We touch on many of these benefits throughout this book, so it's unnecessary to repeat them here.

Why use a terminal multiplexer over a standard terminal app with native tabs (for example, OS X Terminal, iTerm, Gnome Terminal, and so on)? There are many reasons, but the following are a few:

  • The addition of Copy mode allows for copying and pasting between terminal windows without reaching for your mouse. This is not something that can be easily achieved with a standard tabbed terminal application.

  • The addition of the saved state means that you can close a terminal window that is running tmux, and your entire session state will be saved and can be reattached later (assuming your terminal program allows the tmux server to continue running in the background; not all do). Any programs that were running continue running in the background. This is not the case with tabs in iTerm, OS X Terminal, or any Linux terminal. Closing one of these will drop your state for that window.

  • There is much more power and flexibility to using tmux to split a window into panes and rearranging those panes than in using any tabbed terminal interface. Most modern terminal applications will now support simply a vertical or horizontal split, and that's about it. With tmux, the possibilities are (nearly) endless in terms of the number of splits, layouts, and so on.

  • All of the tmux keyboard shortcuts for changing the size and layout of these splits translate into less dependence on your mouse, which is something most normal tabbed terminal applications cannot offer.

  • Not only are there more shortcuts with tmux for tweaking the size of this or that, but tmux also offers a much broader range of customizability in terms of keyboard shortcuts for every operation it can handle. Literally, every single tmux shortcut can be changed or rewired with a simple configuration file, as shown in Chapter 2, Configuring tmux. Some terminal applications might offer some customization, but none are quite as powerful as tmux in this regard.

  • The addition of capabilities in pair programming, setup configuration, and third-party utilities, as we discuss throughout this book, is unachievable with a standard terminal application.

Of course, the list goes on, as you have seen or will see throughout this book, but these are some of the highlights as to why a terminal multiplexer is preferred over a regular terminal app with tabs.

Now that you are most likely sold on using a terminal multiplexer rather than just tabs in a terminal program, why choose tmux over its main competitor, GNU Screen (Screen hereafter)? There are many reasons, but the following are a few:

  • The tmux Command mode was written in order to allow other utilities to control tmux. This is the reason for the rich developer community that has sprung up around it. Screen is more difficult and complicated to control remotely.

  • As a result, the third-party tools for working with tmux are much more prevalent and powerful.

  • The tmux commands are simple utilities, taking a very good page from the UNIX playbook. These small commands can be run via key combinations, by entering the command after the tmux utility, or via tmux's Command mode. Screen is more of a monolith without these nicely separated commands.

  • tmux is newer project, often learning from Screen's mistakes. The first Screen release was way back in 1987. The first tmux release was 22 years later in 2009.

  • The tmux source has fewer lines of code while offering more features.

  • Screen has been a mostly abandoned project for many years. In fact, there was not even a single minor Screen release between 2008 and April, 2014. It had been dormant since before tmux was released and finally had a minor release six years after the last one.

  • tmux has a clean client-server model that allows tmux to keep running when you detach a tmux session. While you can detach from Screen, it has a less defined client-server model.

  • Screen has, essentially, one set of key bindings. In contrast, tmux tries to stay more friendly to developers and includes both vi and Emacs key bindings for most of its commands.

  • The pane and window management in tmux is much simpler and more powerful. With tmux, it is easy to split a window into many different panes, move them around, attach them to different windows, move windows, attach windows to different sessions, and so on. Many of these tasks are not possible in Screen, and they end up being far more complex than they are with tmux.

  • The tmux status bar configuration, out of the box, is quite simple and defaults to what most people would like. Screen, on the other hand, does not include a status bar by default, and the way to specify it is very complex. For example, this is an actual line from my Screen configuration to enable a tmux-style status bar:

    hardstatus string '%{= kG}[ %{G}%H %{g}[%{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'  

No; this is not a joke. This is the actual line and all it does is display the same things that the tmux status bar supports out of the box.

There are more benefits of tmux over Screen, but this should give any reader a good indication of the rationale here.

Note

As it was the first terminal multiplexer we ever used, Screen still has a special place in the author's heart. The intent here is certainly not to bash Screen, which, impressively, is celebrating its 27th year at the time of writing this. Screen is still quite a great program and very much paved the way for tmux. However, you purchased this book because you wanted to learn about tmux and our intent was to enumerate some reasons why tmux is the most awesome program of its type.