tmux is a terminal multiplexer for Unix-like operations systems. I’ve gone years without it with lots of regrets.
I have my projects and work with a team to build products. When I’m not working on a team project, I’m fidgeting with my side hustle. The number of times I’ve started various projects and have had to navigate between the same folders is innumerable.
Perhaps you’re reading this because you want to save yourself from moving back and forth between windows and save time along the way. Let me show you how to get started.
With tmux, you’ll be fine if you know how to use sessions, windows and panes. You create windows inside sessions, and each window can have pane. A pane is a section of a divided window.
In this short write-up, you’ll learn the minimum you need to get productive with tmux. We’ll cover just the basics but by the end of you’ll know how to:
- Manage windows in
- Split panes and change pane layouts.
Knowing these may save you a lot of time and get you productive.
I have a separate session for when I’m writing blog posts and one for work. It feels organised this way. I don’t have to navigate in and out of folders repeatedly.
The installation of tmux is different for each operating system, and you’ll find more information on the tmux repository page.
The tmux Prefix
When you start tmux, you type commands in your loaded shell to run programs. But how do you type commands to tmux? tmux has a keybinding called a “prefix” that you type with additional commands to manage it. This Prefix, by default, is
CTRL + b. Let’s refer to this combination as
Prefix. Anywhere you see
Prefix + x, it’ll mean pressing
CTRL + b simultaneously followed immediately by the
Managing tmux Sessions
My favourite thing about tmux is its sessions. You can have several persistent sessions and pick up where you left off. You can identify sessions by name.
Starting A Session
Here are some ways to start nameless sessions.
tmux tmux new tmux new-session
To start a session with a name, you type:
tmux new -s dev
The above starts a new session called
dev. We haven’t talked about windows yet. We’ll get there soon. To create a named session with a window at the same time, you can run:
tmux new -s dev -n rails
This starts a new session called “dev” and attaches a window called “rails”.
Until this point, if you created a session, tmux jumps into the session right after its creation. But sometimes you want to have a session in the background and not jump into it right away. To do that, you run:
tmux new -s dev -d
Killing A Session
At some point, you’ll be done with a session and would want to get rid of that session. The command for that is:
tmux kill-session -t name-of-session
Attaching To And Detaching From A Session
Depending on how you created your session, you might be in or out of that session at any point.
If you created a named session without
-d, you’re automatically attached to that session, in that case, you might want to get out of (detach) that session and into another session. Here’s how to detach from a session you’re in:
Prefix + d
To enter a session (attach), you’d want to do
tmux attach -t dev
This attaches to the
dev session. If you happen to have only one session, it’s enough to do:
Talking of multiple sessions, let’s see how to list all sessions you’ve created.
Listing tmux Sessions
To list the tmux sessions available, you run:
A shortcut to this is:
Managing tmux Windows
tmux windows are like having tabs in your regular shell session. You can have different windows for various purposes. We mostly create windows with prefixes; this makes sense because we’ve already started tmux. We now need to instruct tmux on what to do, which is what the prefix
CRTL + b is there for.
Prefix + c
creates a new window in the current session.
If you have multiple windows, you can use:
Prefix + n
Prefix + p
to navigate these windows.
n represents “next” while
p gets you to the previous window.
In a session with multiple windows, you have the windows numbered, and in this case, you can press the Prefix with the number associated with the window in the status bar and jump into it.
In the image above, I’ve zoomed in on the windows I have. The windows in this image are named “blob”, “rails” and “jekyll”.
Another way to navigate windows is to type
Prefix + 0..9
0..9 is the number attached to the name of the window.
Listing tmux Windows
Like how you can list what sessions you have at any point, you can also list windows; this makes it easy to navigate your windows in when you have more than a handful of them.
The command to use to get the list of windows above is:
Prefix + w
In this image, you can see I have three sessions “panes”, “siaw23” and “tester”. The windows are under sessions.
If you get overwhelmed and have too many windows to handle, you can search for them using:
Prefix + f
After this step, you type a text you believe is in your target window, and tmux will present you with windows containing that text.
Renaming A Window
You can rename a window with:
Prefix + ,
The status bar then will change to yellow with a cursor, waiting for you to key in the new name.
Closing A Window
To close a window, you press
Prefix + &
This will command will present a “yes” or “no” prompt; it needs a confirmation before closing. To close a window right away without a prompt, you type
Splitting Panes And Changing Layouts
The crux of tmux is the ability to run multiple terminals and windows running on the same screen. There are two ways to split an active screen:
- Horizontally with
Prefix + %
- Vertically with
You can split screens as much as you want. tmux comes with some default layouts that you can cycle through for a perfect match. The layout is how the split panes are arranged on the screen. To cycle through layouts, you type
Prefix + SPACE
Here’s a default list of layouts you get with tmux that you can cycle through with
Prefix + SPACE:
- even-horizontal – Stacks panes horizontally; left to right.
- even-vertical – Stacks panes vertically; top to bottom.
- main-horizontal – Creates one larger pane on the top and smaller panes underneath the larger pane.
- main-vertical – Creates one large pane on the left side of the screen and stacks the rest of the panes on the right vertically.
- tiles – Arranges all panes on the screen evenly.
To close a pane, you type
Prefix + x. You can also move between panes by typing `Prefix + o’.
Clearing The Screen
If you’re like me and you use ZSH, iTerm and
CMD + k to clear your screen, you’ll notice that tmux hangs and starts to misbehave. You can type
clear to clear your screen or
CTRL + l for the same effect, but some of us are just too used to
CMD + k so here’s the fix:
- On iTerm go to “Preferences”.
- Go to “Keys”.
- Click on the “+” sign below the screen.
- “Click to Set”.
- In the “Action” field, pick “Send Text”.
- Then enter
clear\nin the empty field that follows.
- Press “OK”.
- Enjoy your life.
Starting with tmux has some learning curve. The basics on this page present just a few things to memorise until you get the hang of it, from then on, you can find a more advanced resource to upgrade your workflow to the next level.
The trick to these productivity tools and workflows is practice.
I hope this has been helpful. If you have some tips or questions, please leave them in the comments.