Faster command-line workflow with tmux

Lam Bui
Lam Bui
Oct 11, 2017 · 7 min read
Time to go through hyperspace (with tmux)!

As we all grow as developers, learning how to be efficient and improving our workflow is one best things we can do to become better engineers. The faster you can accomplish a task and reduce potential points of error, the more you can get done. Today, I’m going to talk about one of my favorite efficiency tools — tmux

tmux is a command-line terminal multiplexer with session management

In practice, you may have run into situations where you need to view multiple terminal consoles at the same time. For example, you may have a few servers running (e.g. web, database, debugger) and you might want to monitor all the output coming from these servers in real-time to validate behavior or run commands. Or maybe you want to run vim in a terminal window to sling some code and a plain command-line in another window for testing.

Before tmux, you might have just opened a few different tabs in the terminal and switched between them to see the output. Or maybe you resized each terminal window to fit both on the screen at a time:

three(3) separate windows — sadness :(

These methods work, but are a bit clunky and can be annoying to switch between. You would also have to remember which terminal tab corresponds to what server.

Thankfully, there’s an easier way — tmux

So what is tmux and why do I care?

tmux is a command-line terminal multiplexer with session management.

Here is the same setup as above, but using tmux instead:

tmux session with windows for vim, man, and command-line

At its most basic level, tmux allows you to split your single terminal window up into multiple terminal instances(panes). There is also another awesome feature that gives you the ability to save(tmux detach) and retrieve(tmux attach) command-line history from one-or-many terminal sessions on reconnect, which is incredibly powerful — more on this later.

In a nutshell, here are some of the most popular tmux features:

  • Window/Pane management
  • Session management with persistence
  • Sharable sessions with other users
  • Scriptable configurations
  • Plugins

I am a big believer in “learn-by-example,” so let’s explore tmux with some examples.

How does it work?

Client → Server → Session → Window → Panes

Again, tmux is a window service that provides session management.

Here’s a brief walk-through of what is actually happening:

A client(you) connects to a server(local machine or remote). A tmux service is running in the background on that server and will do the magic of handling session/window management. The user then creates a new tmux session to do some work. Each tmux session contains a Window object (but there can be many) and within each Window, there are one (or many) panes.

Window — red box, Pane — blue box

How do I install it?

Linux: use your distribution’s package manager

  • e.g. on Ubuntu-based systems you can do:
sudo apt-get install tmux

Mac: use brew package manager

  • Install brew , if not installed already — https://brew.sh/
  • Then from the command-line:
brew install tmux

Windows: Windows 10 → bash Windows 8 (or older)→ Cygwin

Hardcore status: compile tmux yourself :)

Verify Your Install

You can verify that you have installed tmux correctly with:

man tmux

The terminal will show something like:

man entry for tmux

PRO-TIP: Noticeman tmux shows tmux usage instructions, so make sure to refer back to this as you learn. (Press q to exit the man page by the way)

After installing on your platform of choice, you’re good to go.

Starting a New Session

I mentioned before that tmux is also a session manager, so all output that shows up in all terminals(windows/panes) are captured in a session.

To understand what that means, let’s start by using tmux to create our first session by running the following command:

tmux new-session -s <your_session_name> 

Note: You can give the <your_session_name> any name you wish. It’s just used to make it easier to identify which session you want to attach to. By default, the session naming behavior is incrementing ordinal numbers, if none is specified.

newly created session named ‘test’ — notice the colored bar at the bottom

Great, now you’re in a tmux session. To see available sessions use the following command:

tmux list-sessions 

Now that we have a session running, let’s get to exploring some features. I’m going to cover the two most useful ones below through some common use cases.

Controlling a Session

With tmux there are a few commands that are useful to know from the start to be able to use tmux effectively. In tmux, actions are accomplished via command-mode(also called prefixing). So, anytime you want to do something tmux related(e.g. splitting panes), you must first put tmux in command-mode to do so and then execute your command(action).

Using command mode:
Enter command-mode: ctrl-b
Enter text command-mode: ctrl-b :
Exit command-mode: esc
List command-mode shortcuts: ctrl-b ?

So, the process is as follows:

  1. Enter command mode
  2. Issue your tmux command
  3. Exit command mode
  4. Do your work
  5. Repeat as needed

Use case: Split Panes

Say you have a need to view multiple terminal windows at a time. I mentioned before the example of running vim then having a terminal to type your commands in. Maybe you also want to monitor your server at the same time as well. Let’s take a look at how we can use panes to help us do that:

  1. Start a tmux session:
    tmux new-session -s <your_session_name>
  2. To split vertically:
    ctrl-b %
  3. To split horizontally:
    ctrl-b "
  4. To navigate and select a pane:
    ctrl-b <arrow keys>
  5. To toggle full-screen zoom in/out on the current pane:
    ctrl-b z
  6. To close the current pane:
    ctrl-b x then confirm with y or n
Split panes example

Use case: Session Management

I mentioned before that session management is one of the most powerful features of tmux. Sessions allow you to create persistent workspaces that you can connect to and disconnect from at will.

Here is a hypothetical scenario:

Let’s say you are at the office and connected to a remote server via SSH. You might be running a test that is printing logs out to the command-line, but it takes a long time(hours). It’s 6pm and you need to get home to feed the dog, but you don’t want to lose your session data when you reconnect again.

Or maybe you accidentally closed your terminal window — yikes!

tmux to the rescue! tmux gives you session management for free. Every tmux session is automatically logged and history is retained. You just need to reattach to the session and you have all that work back in addition to the output that happened when you were on your way home or after you accidentally closed your terminal window. Pretty rad, huh?

Here are the steps:

  1. Start with a named session:
    tmux new-session -s <your_session_name>
  2. Do your work in your tmux session
  3. Detach from your session when you are done:
    tmux detach
  4. [Optional] View available attachable sessions:
    tmux list-sessions or tmux ls
  5. Reattach to your tmux session when you are ready to continue working:
    tmux attach -t <your_session_name>
Session management example

Additional Details/Handy Shortcuts

tmux is an extremely powerful and useful tool that can supercharge your command-line workflow. I encourage you to give tmux a shot and challenge you to discover a configuration and workflow that fits your personal style.

The only way to get better is to practice by using tmux in your daily workflow. Below are additional tips and reference material to help you along your tmux and workflow efficiency journey.

Common Shortcuts

Enter text command-mode: ctrl-b :
Enter command-mode: ctrl-b
Exit command-mode: esc
List command-mode shortcuts: ctrl-b ?
Toggle full screen current pane (zoom): ctrl-b z
Enable scroll mode: ctrl-b [

PRO-TIP: when in doubt, the command: ‘man tmux’ is your friend

Other Tool Options

Though I really like tmux there are a few other tools out there that accomplish similar goals as well.

GNU Screen — available on most platforms (open-source software)
https://www.gnu.org/software/screen/
Window manager from GNU

iTerm2 — OS X only
https://www.iterm2.com/
Replacement for the default Terminal application

Note: One nice thing that I want to mention about tmux in particular, is that it is available on most platforms so you can count on it being a quick install away anywhere you need it.

More Reading

tmux official repository
https://github.com/tmux/tmux
Check this out if you are curious about the tmux implementation or are interested in recompiling the code yourself

tmux resource repo
https://github.com/rothgar/awesome-tmux
List of helpful links, tutorials, and resources

The tao of tmux
https://leanpub.com/the-tao-of-tmux/read
Excellent free book covering more advancedtmux features

tmux cheat sheet
https://gist.github.com/MohamedAlaa/2961058
Very handy list of tmux shortcuts

A tmux crash course
https://robots.thoughtbot.com/a-tmux-crash-course
Learn more tmux basics

A quick and easy guide to tmux
http://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/
Excellent tmux tutorial

VIM Adventures
https://vim-adventures.com/
Learn vim by playing a game

Thanks to Albrey Brown and Tiffany Pham

Lam Bui

Written by

Lam Bui

Software engineer fascinated with building tools and automation to make people more efficient

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade