When I was studying at the Flatiron School a little while ago, Tute Costa gave a talk about refactoring like a pro. During his presentation, as he edited code, ran tests, and saved files, he pretty much never left the command line. How did he do that? Well, he used tmux. It’s a terminal multiplexer, an app that lets you access multiple command-line interfaces at once by dividing the terminal window into multiple sections, like this:
Want to try it out? First, install tmux using homebrew:
$ brew install tmux
From there, all you need to do to get tmux running is this:
And now you’re in. Your terminal should look pretty much the same, but now with a green, no-nonsense status bar running along the bottom.
All tmux commands are initiated using a “prefix”, which, by default, is ctrl+b. (It’s popular to reconfigure the tmux prefix to ctrl+a, however, because reaching the A key from the control key is much easier on the fingers. Here’s how.)
Panes are what divide the Terminal window into different sections and allow you to access multiple command-line interfaces simultaneously.
To split the current window vertically, hit prefix % (using the default configuration, that’s ctrl+b then shift+5). Your original command line interface will now be in one pane on the left, and a new command line interface will appear on the right.
You can use either of these panes independently, just as if each were in its own Terminal tab.
Now, to split the current pane horizontally, creating a new pane below, hit prefix “.
Get as crazy as you want with this:
But don’t touch your trackpad. It won’t do you any good. You’re navigating from the keyboard like a real-life programmer now. So if you want to move from one pane to another, you need to you use prefix+<arrow>. You can also cycle through panes using prefix+o. Want to see a pane in full screen? Just hit prefix+z to zoom in, then hit it again to zoom back out.
A group of panes — whether it’s a group of many panes or just one—makes up a window. Each window is listed at the left of the status bar by index number along with the program that its current pane is running. To create a new window, hit prefix-c.
This displays a fresh, newly-created window. It also adds it to the list of windows on the status bar with an index number and the program its current pane is running. The open window is marked with a star.
You can create as many windows as you want…
To navigate to the previous window, hit prefix+p. To navigate to the next window, hit prefix+n. To jump to any window, hit prefix+<window index number>.
A session is a collection of all the windows listed on the status bar. When you’re using a given session, you’re “attached” to it. Just like you can maintain multiple panes per window, and multiple windows per session, you can also maintain multiple sessions per Terminal.
To “detatch” from the current session, hit prefix-d. This will exit tmux while maintaining any open session exactly as it is. To open a new session, just initiate tmux once again.
To see a list of all tmux sessions, first detach. Then, enter:
$ tmux ls
This will display all open tmux sessions, listed by index number:
To attach any of the sessions to the terminal, “target” that session with
$ tmux attach -t <session number>
To delete, or “kill”, a current window, hit prefix &. You can also type “exit” to delete the current pane. If there’s only one pane in a window, “exit” will also kill the window. If you’re in the last window of the session, “exit” will kill the current session.
Tmux is just the beginning of setting up camp in Terminal. In combination with a Vim editor, you can use tmux to edit, test, and save code without ever leaving the command line or your keyboard. For more tmux tricks, check out this handy command cheat sheet, or, for deeper dives, here are the tmux manual and the tmux Pragmatic Programmer’s guide. Happy multiplexing.