Have you ever found yourself in a situation where multiple Tmux sessions and windows open, and you need to switch between them quickly? Tmux is a terminal multiplexer tool that allows you to create multiple sessions and windows, making it easier to multitask. However, if you’re unfamiliar with the shortcuts and commands to navigate between sessions and windows, it can be a frustrating experience.
This article will cover the basics of Tmux sessions and windows and show you how to switch between them quickly and efficiently. Whether you’re a Tmux beginner or a seasoned user, you’ll find valuable tips and tricks to make your workflow smoother and more productive.
Understanding Tmux sessions and windows
Before we dive into the details of switching between Tmux sessions and windows, let’s first understand what they are and how they work.
A Tmux session is a container that holds one or more Tmux windows. Each session has a unique identifier, which you can attach to it from another terminal or detach from it to return to the shell. When you start a new session, Tmux will automatically create a new window and attach it to the session. You can then create additional windows within the session as needed.
A Tmux window is a container that holds one or more Tmux panes. Each window has a unique identifier, which you can use to switch between windows within the same session or between sessions. When you create a new window, Tmux will automatically attach a new pane to the window. You can then split the pane into multiple panes as needed.
Switching between Tmux sessions
Switching between Tmux sessions is a two-step process: detaching from the current session and attaching to the target session. To detach from the ongoing session, use the following command:
C-b d
This command will detach you from the current session and return you to the shell.
Detach from session
You can then use the following command to attach to the target session:
tmux attach-session -t target-session
Replace “target-session” with the name or ID of the session you want to attach to.
Tmux attach the previously detached session
If you’re not sure what the name or ID is, you can use the following command to list all the sessions:
tmux list-sessions
This command will display a list of sessions’ names and IDs.
List sessions
Alternatively, you can use the following shortcut to switch to the last active session:
C-b (
This command will cycle through all the available sessions and switch to the last active one.
Switching between Tmux windows
Switching between Tmux windows is a similar two-step process: selecting the target window and switching to it. To choose the target window, use one of the following commands:
C-b n # switch to the next window C-b p # switch to the previous window C-b 0 # switch to window 0 C-b 1 # switch to window 1 C-b 2 # switch to window 2
These commands will cycle through all the windows within the same session and switch to the target window.
Alternatively, you can employ the following command to display a list of all the windows within the same session:
C-b w
This command will display a list of all the windows, along with their names and numbers. You can then employ the arrow keys to select the target window and press Enter to switch to it.
List all windows in the current session
Switching between Tmux panes
Switching between Tmux panes is similar to switching between windows but with an additional step: selecting the target pane within the target window. To choose the target pane, use one of the following commands:
C-b Up Arrow # switch to the pane above the current one C-b Down Arrow # switch to the pane below the current one C-b Left Arrow # switch to the pane to the left of the current one C-b Right Arrow # switch to the pane to the right of the current one
These commands will cycle through all the panes within the same window and switch to the target pane.
Alternatively, you can employ the following command to display a list of all the panes within the same window:
C-b q
This command will display a list of all the panes and their numbers. You can then enter the number of the target pane to switch to it.
List of all panes
Switching between Tmux sessions and windows quickly
While the above commands are useful for switching between Tmux sessions, windows, and panes, they can be time-consuming if you have many of them open. Here are some tips and tricks to switch between Tmux sessions and windows quickly and efficiently.
Use shortcuts to switch between recent sessions
Tmux allows you to switch between the most recently used sessions using the following command:
C-b L
This command will switch to the last session you used, regardless of whether it was detached or attached. You can press the command repeatedly to cycle through all the recent sessions.
Switch between recent sessions
Use prefix-based shortcuts to switch between windows
Tmux lets you use prefix-based shortcuts to switch between windows within the same session quickly. By default, the prefix key is “C-b,” but you can customize it.
Here are some useful prefix-based shortcuts for switching between windows:
C-b c # create a new window C-b w # list all windows C-b % # split the current pane vertically C-b " # split the current pane horizontally C-b <number> # switch to window <number> C-b n # switch to the next window C-b p # switch to the previous window
You can also customize the shortcuts using the Tmux configuration file. For example, you can add the following lines to the file to switch to windows using the Alt key and number keys:
# Alt + <number> to switch to window bind-key -n M-1 select-window -t 1 bind-key -n M-2 select-window -t 2 bind-key -n M-3 select-window -t 3 bind-key -n M-4 select-window -t 4 bind-key -n M-5 select-window -t 5
Prefix-based shortcuts to switch between windows
Use prefix-based shortcuts to switch between panes
Tmux allows you to use prefix-based shortcuts to switch between panes within the same window quickly. Here are some useful prefix-based shortcuts for changing between panes:
C-b % # split the current pane vertically C-b " # split the current pane horizontally C-b o # switch to the next pane C-b ; # switch to the last active pane C-b Up Arrow # switch to the pane above the current one C-b Down Arrow # switch to the pane below the current one C-b Left Arrow # switch to the pane to the left of the current one C-b Right Arrow # switch to the pane to the right of the current one
You can also customize the shortcuts using the Tmux configuration file. For example, you can add the following lines to the file to switch to panes using the Ctrl key and arrow keys:
# Ctrl + <arrow key> to switch between panes bind-key -n C-Up select-pane -U bind-key -n C-Down select-pane -D bind-key -n C-Left select-pane -L bind-key -n C-Right select-pane -R
Prefix-based shortcuts to switch between panes
Another worth noting is that Tmux allows you to switch between sessions, windows, and panes using the mouse. By default, clicking the status bar with the left mouse button brings up the list of available Tmux sessions. You can then select a session by clicking on it.
Similarly, clicking on a window or pane in the status bar will switch to that window or pane. You can also resize panes by clicking and dragging on the divider lines between them.
To enable mouse support in Tmux, add the following line to your Tmux configuration file:
set -g mouse on
Enable mouse support mode
With mouse support enabled, you can use the mouse to switch between Tmux sessions, windows, and panes, resize panes and perform other actions.
However, it’s worth noting that some users prefer to use Tmux entirely through keyboard shortcuts, as it can be faster and more efficient once you become familiar with the commands. Additionally, some terminal emulators may not support mouse input, so relying on keyboard shortcuts can ensure consistent behavior across different environments.
Overall, Tmux offers versatile tools for managing your terminal workflow, whether using the mouse or sticking to keyboard shortcuts. By mastering the various commands and shortcuts, you can streamline your workflow and work more efficiently in the terminal.
Conclusion
Tmux is a powerful tool for managing terminal sessions, windows, and panes. With its robust set of commands and customizable shortcuts, it allows you to work more efficiently and productively.
This article covers some of the essential commands and shortcuts for quickly switching between Tmux sessions, windows, and panes. Whether you’re new to Tmux or an experienced user, these tips and tricks should help you get the most out of this powerful tool.
Remember, the key to using Tmux effectively is practicing it regularly. As you become more comfortable with its commands and shortcuts, you’ll find that your workflow becomes smoother and more streamlined.
We hope this article has helped show you how to switch between Tmux sessions and windows quickly. Thanks for reading, and happy Tmux-ing!
tmux cheat sheet
(C-x means ctrl+x, M-x means alt+x)
Prefix key
The default prefix is C-b. If you (or your muscle memory) prefer C-a, you need to add this to ~/.tmux.conf
:
# remap prefix to Control + a
set -g prefix C-a
# bind 'C-a C-a' to type 'C-a'
bind C-a send-prefix
unbind C-b
I’m going to assume that C-a is your prefix.
Sessions, windows, panes
Session is a set of windows, plus a notion of which window is current.
Window is a single screen covered with panes. (Once might compare it to a ‘virtual desktop’ or a ‘space’.)
Pane is a rectangular part of a window that runs a specific command, e.g. a shell.
Getting help
Display a list of keyboard shortcuts:
C-a ?
Navigate using Vim or Emacs shortcuts, depending on the value of mode-keys
. Emacs is the default, and if you want Vim shortcuts for help and copy modes (e.g. j, k, C-u, C-d), add the following line to ~/.tmux.conf
:
setw -g mode-keys vi
Any command mentioned in this list can be executed as tmux something
or C-a :something
(or added to ~/.tmux.conf
).
Managing sessions
Creating a session:
tmux new-session -s work
Create a new session that shares all windows with an existing session, but has its own separate notion of which window is current:
tmux new-session -s work2 -t work
Attach to a session:
tmux attach -t work
Detach from a session: C-a d
.
Switch between sessions:
C-a ( previous session
C-a ) next session
C-a L ‘last’ (previously used) session
C-a s choose a session from a list
Other:
C-a $ rename the current session
C-a
Managing windows
Create a window:
C-a c create a new window
Switch between windows:
C-a 1 ... switch to window 1, ..., 9, 0
C-a 9
C-a 0
C-a p previous window
C-a n next window
C-a l ‘last’ (previously used) window
C-a w choose window from a list
Switch between windows with a twist:
C-a M-n next window with a bell, activity or
content alert
C-a M-p previous such window
Other:
C-a , rename the current window
C-a & kill the current window
Managing split panes
Creating a new pane by splitting an existing one:
C-a " split vertically (top/bottom)
C-a % split horizontally (left/right)
Switching between panes:
C-a left go to the next pane on the left
C-a right (or one of these other directions)
C-a up
C-a down
C-a o go to the next pane (cycle through all of them)
C-a ; go to the ‘last’ (previously used) pane
Moving panes around:
C-a { move the current pane to the previous position
C-a } move the current pane to the next position
C-a C-o rotate window ‘up’ (i.e. move all panes)
C-a M-o rotate window ‘down’
C-a ! move the current pane into a new separate
window (‘break pane’)
C-a :move-pane -t :3.2
split window 3's pane 2 and move the current pane there
Resizing panes:
C-a M-up, C-a M-down, C-a M-left, C-a M-right
resize by 5 rows/columns
C-a C-up, C-a C-down, C-a C-left, C-a C-right
resize by 1 row/column
Applying predefined layouts:
C-a M-1 switch to even-horizontal layout
C-a M-2 switch to even-vertical layout
C-a M-3 switch to main-horizontal layout
C-a M-4 switch to main-vertical layout
C-a M-5 switch to tiled layout
C-a space switch to the next layout
Other:
C-a x kill the current pane
C-a q display pane numbers for a short while
Other config file settings
Force a reload of the config file on C-a r:
unbind r
bind r source-file ~/.tmux.conf
Some other settings that I use:
setw -g xterm-keys on
With tmux, you can create multiple terminal sessions that can be opened (attached) and closed (detached) or displayed simultaneously, all from one window.
Learning how to use a terminal multiplexer will save you a lot of headaches if you spend a lot of time in the terminal. This is especially true if your work involves accessing remote machines via command line only.
Screen vs tmux
Tmux is considered to be the next evolutionary step forward from the GNU Screen multiplexer.
If you’re used to the screen command, you’ll find it easy to get started right away. There are many similarities between the two applications. We had a great response to our screen command article, and I feel like our readers will really enjoy using some of the updated features of tmux.
📋
If you are already comfortable with the screen command, you can quickly scan this article for the differences. For some commands, I may point out similarities or differences. Aside from these notes, this article will not presume any previous knowledge.
If you are a little lost and don’t know what this article is about yet, that is totally fine. We’re going to walk through everything together. I will show you how to install tmux and how to perform basic operations.
Install tmux
Tmux is a popular application, and it should be available in your Linux distribution’s official repository. You can install it using the package manager of your distribution.
On Debian and Ubuntu-based distributions, you can install it using the following command:
sudo apt install tmux
Using tmux
As we touched on, tmux is a great application to make you more productive. It is a powerful piece of software, but it can be a little confusing to get started with.
I will guide you through critical features one at a time. My goal is to ease you in and show you the basic functionality. There are so many ways to customize tmux, that we won’t be able to cover everything in this article. If you are curious about an article on advanced features and functionalities, let us know in the comment section!
Create a session in tmux
Open a terminal and use the following command:
christopher@linuxhandbook:~$ tmux
This should bring you into tmux. You will see a command prompt as usual, but you will now see a taskbar style menu on the bottom of the terminal that will say something like 0:bash*
. The asterisk indicates that this is your active window.
Let’s create a couple of windows that you can switch between. You can do this by using the prefix [CTRL + b] + c.
You should see on the taskbar that something named 1:bash has been added. Let’s add two more before you continue further.
It should now look like the screenshot below. You should have a total of four new terminal windows (you’ll only see one window at a time).
The automatic window name
Tmux can actually identify certain programs and rename your windows automatically (if you haven’t named your window explicitly). For example, if you run the top command in a window, you’ll notice that the window name is changed from bash to top.
Session vs Window vs Pane (split) in tmux
Before you go any further, you should know the difference between session, window, and pane in tmux.
💡
Consider the sessions as notebooks, windows as chapters, and window panes as its pages.
A session can have multiple windows. A window is basically a terminal. You can split a window between several vertical and horizontal panes. This means a terminal window can have multiple terminals in it.
You can create a tmux session for a specific topic/project. For example, if you manage three servers, maybe you can create one session for each server to perform tasks. Use a window for monitoring system resources and another for performing some maintenance tasks. The system monitoring window can have multiple panes to monitor memory, CPU, and disk usage.
Switching between windows in a tmux session
Remember that CTRL+b (simultaneously) is like your “tmux key”. It tells the software that you want to enter a command. Look at the table below to get started navigating the tmux interface.
Go to Previous Window | Go to Next Window | Go to Nth Window |
---|---|---|
[CTRL + b] + p | [CTRL + b] + n | [CTRL + b] + {0, 1, 2, 3, etc.} |
Now you can move “back and forth” or select a specific instance by its ID number. Try those things out until it feels comfortable.
Ready? Awesome. Let’s check out some of the other functionalities.
📋
If you are an experienced Screen user, you may be used to using the [CTRL + a] prefix. Editing the configuration file allows you to change the prefix if you would like. I actually found the alternate key combo improved my efficiency, but I also understand old habits are hard to break.
Switch using window list
Another option uses [CTRL + b] + w. This gives you a visual overview of sessions. You can use the arrow keys to select the desired windows/panes.
0:00
/0:07
Using interactive window switch in Tmux
Name or rename windows in tmux
You can name your current window with the following key combination:
[CTRL + b] + ,
Name your tmux session
You may find it helpful to name your sessions with meaningful titles to keep things organized. Let’s try naming your first session with tmux.
You can name it anything that we want, but in this case, I will name it “free”. Enter the following command:
tmux new -s free
You should now have a new session of tmux running. If you look in the bottom-left area of the window, you will see the name of your session rather than the generic ‘bash’.
If you already created a session, you may name it or rename it using the following key combination:
[ctrl+b] + $
Detach a session (come out of tmux session but keep it running in the background)
Before you continue, let’s begin running free with the watch command, which will update the results every 2 seconds.
watch free
If you are not familiar with free or watch, I recommend that you check out the watch and free command. Knowledge of their functionality, however, is not essential for using them with tmux.
Okay, so once the program begins, go ahead and detach the session. Use this key combination:
[ctrl + b] + d
This should return you to a standard command prompt. Keep in mind that the tmux session is still running in the background, and you can enter it again whenever you want.
List tmux sessions
What happened to your session? It is still running in the background. You can re-open the session by name or by ID number, but what if you forget the session name?
There is a list function built into tmux:
tmux ls
This will list all your current tmux sessions. Running it will produce output like this:
christopher@linuxhandbook:~$ tmux ls
free: 1 windows (created Sat Feb 29 03:16:31 2020) [80x23]
As you can see, it also lists how many windows a session has.
Re-enter a session in Tmux (also known as attach)
To reopen your tmux session, you can use the tmux command with attach or attach-session option like this:
tmux attach -t session_name_or_number
Here is an animation that shows all these steps until this point in the demonstration.
Tmux Commands Animated Overview
Creating multiple panes in tmux
Now that you have an understanding of the basics, you can go further and create multiple panes.
Horizontal (Left/Right) | Vertical (Top/Bottom) |
---|---|
[CTRL + b] + % | [CTRL + b] + « |
Let’s get some practice with these commands by creating a vertical split followed by a horizontal split.
[ctrl + b] + "
[ctrl + b] + %
If you’ve entered the commands in that order, you should have three panes that look this:
You can use [CTRL + b] + space key to toggle the layout of the currently focused pane.
Switching between panes
To switch between the splits you can use the tmux prefix [CTRL + b] + arrow keys.
[ctrl+b] + arrow keys
This cycles the panes in the direction you choose. The selected pane will be highlighted in green.
Zoom into a pane
You can also “zoom” into a selected pane with the [CTRL +b ] + z. This will bring the selected screen to full size. To exit the zoom mode, hit [CTRL + b] + z again.
Killing or closing panes
This is an important one. You can close a pane (splits) by using the prefix [CTRL + b] + x.
It will ask you for the confirmation:
kill-pane 0? (y/n)
Killing or closing windows in tmux
In order to close the current window (tabs), use:
[ctrl + b] + &
It will ask for your confirmation:
kill-window window_name? (y/n)
Killing sessions in tmux
To kill the current tmux session you are using, you’ll have to kill all the windows first. That’s not very convenient, is it?
You can selectively kill a tmux session using its name or number like this:
tmux kill-session -t session_name_or_number
It will kill the session even if you are inside the session.
If you want to kill all the sessions except the one you are currently using (or the last one you used), use this command:
tmux kill-session -a
To kill all tmux sessions in one go, you can use this magical command:
tmux kill-server
Download tmux cheat sheet
You learned the basics of tmux but remembering all the commands is not easy, specially in the beginning. This is why I am summarizing all the commands in the table below. You may also download it in PDF form.
Commands and Key Combinations | Explanation |
---|---|
tmux | Start new session |
tmux new -s name | Start new session with name |
tmux ls | List all the sessions |
tmux kill-session -t name | Kill the mentioned session |
tmux kill-session -a | Kill all sessions except the current or last used session |
tmux kill-server | Kill all tmux sessions |
tmux attach -t name | Attach/enter the mentioned session |
[CTRL + b] + d | Detach/exit the current session |
[CTRL + b] + $ | Name or rename current session |
[CTRL + b] + c | Create new window |
[CTRL + b] + , | Name or rename current window |
[CTRL + b] + w | List all windows |
[CTRL + b] + & | Kill the current window |
[CTRL + b] + n/p/N | Move to next/previous/Nth window |
[CTRL + b] + % | Create horizontal pane (New left and right panes) |
[CTRL + b] + “ | Create vertical pane (New top and bottom panes) |
[CTRL + b] + space key | Toggle the layout of the current panes |
[CTRL + b] + z | Zoom into the current pane |
[CTRL + b] + x | Kill the current pane |
Conclusion
Thank you for following along with our introduction to tmux. As you can see, there are a wide variety of applications. I hope that this basic overview has given you some ideas about how you can use tmux to improve your workflow.
We’ll be covering the steps discussed here in a bit more detail, in their dedicated page. You can find them from the left sidebar of this page.
As always, if you have any questions, please leave them below in the comment section.
Cheat sheet for the open-source terminal multiplexer tmux
. Get started with tmux
: This cheat sheet provides essential tmux
commands for session, window, and pane management, helping you to navigate, customize, and optimize your workflow.
- Published:
- Updated:
- Reading time:
- Author:
- Tags:
- tmux, cheat sheet
- License:
- CC BY-SA 4.0
- Permalink:
- Tmux cheat sheet
Leader #
The leader (also called prefix) — <C-b>
— stands for the key combination Ctrl
+b
.
Session management #
Start a new session:
Start a new session with a name:
tmux new-session -s session_name
Attach to an existing session:
tmux attach-session -t session_name
List existing sessions:
Detach from the current session (<C-b>
+d
):
Switch between sessions:
tmux switch-client -t session_name
Command | Explanation |
---|---|
<C-b> +s |
list sessions |
<C-b> +$ |
name sessions |
Window management #
Create a new window (<C-b>
+c
):
Switch between windows (<C-b>
+1
, <C-b>
+2
, etc.):
Rename the current window (<C-b>
+,
):
tmux rename-window new_name
Close the current window (<C-b>
+&
):
Command | Explanation |
---|---|
<C-b> +c |
create new window |
<C-b> +n |
go to next window |
<C-b> +p |
go to previous window |
<C-b> +[i] |
go to [i] window |
<C-b> +' |
select the window index |
<C-b> +w |
interactive index to choose window (0-9a-z) |
<C-b> +f |
find window by name |
<C-b> +l |
go to last used window |
<C-b> +. |
interactively move window |
<C-b> +, |
rename current window |
<C-b> +& |
close current window |
Pane management #
Split the current pane into two, left and right. (<C-b>
+%
):
Split the current pane into two, top and bottom (<C-b>
+"
):
Command | Explanation |
---|---|
<C-b> +% |
split current pane into two, left and right |
<C-b> +" |
split current pane into two, top and bottom |
Moving between panes #
Navigate between panes (<C-b>
+ arrow keys or <C-b>
+q
):
tmux select-pane -[U/D/L/R]
Command | Explanation |
---|---|
<C-b> +[↑|↓|→|←] |
move through panes |
<C-b> +o |
go to next pane (cycle) |
<C-b> +; |
go to last used pane |
Resizing panes #
Resize panes <C-b>
+ hold down Ctrl
, then use arrow keys:
tmux resize-pane -[U/D/L/R] 10
Zoom in/out of the current pane (<C-b>
+z
):
Command | Explanation |
---|---|
<C-b> + Esc+[↑|↓|→|←] |
Change size of active pane |
<C-b> + Alt+[↑|↓|→|←] |
Change size of active pane |
Command Mode #
Enter command mode (<C-b>
+:
):
Kill a pane or window in command mode:
kill-pane # (or kill-window)
Miscellaneous #
Show tmux help / list all key bindings (<C-b>
+?
):
Reload tmux configuration:
tmux source-file ~/.tmux.conf
These commands should help you get started with tmux.
Customize the key bindings and settings in your ~/.tmux.conf
file to suit your preferences.
Tmux has many features. For advanced usage, see the official documentation.
Further readings #
Sources and recommended, further resources on the topic:
- tmux website
- Getting started with tmux
- tmux manual
Author
Jonas Jared Jacek (J15k)
Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/License
Tmux cheat sheet by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/tmux-cheat-sheet">Tmux cheat sheet</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the Ditig legal page.
I try my best to not horde browser tabs. Occasionally I look up and realize that I have so many open that I now have to scroll. While I attempt to do the same with my tmux
windows (and GNU screen too), there are days where I keep opening news ones, well past the 0-9 range.
Cycle through tmux windows
When this happens, switching with Ctrl+b 0-9
stops being a thing. This makes getting back to these higher numbered windows a bit of a chore.
First, you could use Ctrl+b n
to move to the next window in tmux. It gets the job done. But, it can be tedious if you’re trying to say go from window 1 to window 20. You could always jump to window 9 and start from there.
Jump to a specific tmux window
Another method would be to use Ctrl+b '
(single quote), which will prompt you for the index of the window. You can use this method to jump to any window, by its index, not just the windows numbered 10+.
Both of these method are sufficient enough if you aren’t working with many windows numbered 10+. If you’re like me, you try to avoid those higher numbered windows. This methods ends up being more than enough to get back to a window so you can then close it.
Browse tmux windows
Let’s say you’re a maniac that can actually keep more than 10 windows open and straight in your head. There’s a better way to navigate between those higher numbered windows.
Using Ctrl+b w
will open up a list of your open windows, and you can cycle through them with a preview and pick which one you’d like to jump to:
I use something similar to navigate between my tmux
sessions. While it does work, it’s a bit more work than just being able to use Ctrl+b 0-9
, which is precisely why I like to keep my open tmux
windows below 10 at all times.