Tactical tmux: The 10 Most Important Commands

tmuxpanes

tmux lets you keep things running persistently on servers, so you can disconnect and connect as needed without interrupting tasks that are in progress.

Use-cases: compiling code, running security scans, etc.

It’s best to install tmux using your existing OS package management options.

macOS

$ brew install tmux

Ubuntu Linux

$ apt install tmux

If you invoke tmux by itself, you’ll get dropped into a new session. And if you exit this session, you drop right back into your normal shell.

The ctrl–b shortcut is good to memorize.

$ tmux

Creating named sessions lets you reattach more easily.

$ tmux new -s session-name

A raw tmux session

You want a pause between the shortcut and the following command.

To detach from a session you invoke the shortcut (ctrl-b)—followed by d, for detatch, or by typing detach explicitly.

$ ctrl–b–d

$ tmux detach

Consider remapping CAPSLOCK to CONTROL in your OS to make this easier.

You can—and often will—have multiple tmux sessions on a single system, so you want to be able to see what they are.

You can also show sessions using the shortcut ctrl–b–s.

$ tmux ls

A view of running sessions

Now that we can see those sessions, you can either connect to one by session name, or by number.

Unsupervised Learning — Security, Tech, and AI in 10 minutes…

Get a weekly breakdown of what's happening in security and tech—and why it matters.

The session names start at 0 and increment upwards.

$ tmux attach -t 0

tmux a will connect you to the first available session.

$ tmux attach -t session-name

There are times when you’ll want to destroy a session outright, and that can be done similar to attaching to one.

$ tmux kill-session -t session-name

You can also kill tmux altogether with killall tmux.

tmuxnesting

I don’t use this functionality myself.

Another feature of tmux is the ability to break your session into more discreet components, called windows and panes. These are good for organizing multiple activities in a logical way.

Basically, tmux sessions have windows, and windows have panes. Here’s how I conceptualize the structure.

  • Sessions are for an overall theme, such as work, or experimentation, or sysadmin.

  • Windows are for projects within that theme. So perhaps within your experimentation session you have a window titled noderestapi, and one titled lua sample.

  • Panes are for views within your current project. So within your sysadmin session, which has a logs window, you may have a few panes for access logs, error logs, and system logs.

These all play off of the ctrl-b shortcut.

Basics

  • ? get help

Session management

  • s list sessions

  • $ rename the current session

  • d detach from the current session

Windows

  • c create a new window

  • , rename the current window

  • w list windows

  • % split horizontally

  • " split vertically

  • n change to the next window

  • p change to the previous window

  • 0 to 9 select windows 0 through 9

Panes

  • % create a horizontal pane

  • " create a vertical pane

  • h move to the left pane. *

  • j move to the pane below *

  • l move to the right pane *

  • k move to the pane above *

  • q show pane numbers

  • o toggle between panes

  • } swap with next pane

  • { swap with previous pane

  • ! break the pane out of the window

  • x kill the current pane

Parting thoughts

Here are a few tips I’ve picked up over the years using tmux.

  1. Consider using as few sessions and windows as possible. Humans aren’t as good at multitasking as we think we are, and while it feels powerful to have 47 panes open it’s usually not as functional as you’d imagine.

  2. When you do use windows and panes, take the time to name them. They are indeed useful, but switching between sessions and windows is supremely annoying when they’re all labeled 0, 1, and 2.

  3. Start with a basic config and get used to it before you get silly with it. I’ve seen multiple people spend hours configuring vim or tmux only to confuse themselves and abandon the project altogether. Start simple.

Notes

  1. tmux is a lot like screen, only better. The short answer for how it’s better is that tmux is 1) Tmux is built to be truly client/server; screen emulates this behavior, 2) Tmux supports both emacs and vim shortcuts, 3) Tmux supports auto-renaming windows, 4) Tmux is highly scriptable, 5) Window splitting is more advanced in tmux

  2. The man page.

Related posts: