Tmux is a great tool for managing multiple terminal sessions. However, it does not save your sessions when you restart your computer. This tutorial will discuss how to easily save and restore your Tmux environment after system restarts in Linux.
Introduction
By default, Tmux sessions will not persist across system reboots. This is because Tmux sessions are stored in the operating system's memory, and when the system reboots, all memory is cleared.
This can be frustrating if you're running multiple sessions, windows, panes, and programs.
Of course, there are helpful tools that can make Tmux environments persistent across restarts, but they can be tricky to set up and maintain.
In this article, we will learn about two Tmux plugins that work together to save and restore your Tmux sessions with minimal configuration:
- tmux-resurrect: Allows you to easily save and restore your Tmux environment after system restarts.
- tmux-continuum: Automates the saving and restoring of your Tmux environment. making it even more convenient.
These plugins will automatically save your Tmux sessions when you exit Tmux, and then restore them when you start Tmux again.
1. Save and Restore Tmux Environments with Tmux-resurrect
1.1. What is Tmux-resurrect?
The tmux-resurrect plugin saves all the details of your Tmux environment, so you can easily restore it after a system restart or whenever you need to. It's quite easy to set up and use, and it's designed to make you feel like you never left Tmux.
This plugin goes above and beyond to save and restore the following elements of your Tmux setup:
- All sessions, windows, and panes, along with their order.
- The current working directory for each pane.
- Precise pane layouts within windows, even when zoomed.
- Active and alternative sessions.
- Active and alternative windows for each session.
- Windows with the current focus.
- Active panes within each window.
- "Grouped sessions" for efficient multi-monitor usage.
- And it can even restore programs running within a pane!
One important note is that tmux-resurrect is idempotent, meaning it won't try to restore panes or windows that already exist. The only exception is when Tmux starts with only one pane to restore the previous Tmux environment; in this case, that single pane will be overwritten. This ensures that your Tmux environment remains clean and consistent.
In summary, tmux-resurrect is a must-have plugin for anyone using Tmux, as it simplifies the process of saving and restoring your Tmux sessions, making it easier to pick up where you left off, even after a system restart.
1.2. Install Tmux-resurrect
The requirements for using tmux-resurrect are relatively simple: you'll need Tmux version 1.9 or higher and Bash. It works seamlessly on Linux, OSX, and Cygwin systems.
Tmux-resurrect can be easily installed with Tmux Plugin Manager (TPM). If you haven't installed TPM yet, refer the following link:
Once TPM installed, add the following to your .tmux.conf
to enable tmux-resurrect:
set -g @plugin 'tmux-plugins/tmux-resurrect'
Save the file and close it.
While in tmux session, press prefix + I
(capital i, as in Install) to download and install the plugin.
1.3. Saving Tmux Environment
To save your Tmux environment, press Prefix + Ctrl-s
key combination.
Heads Up: The default prefix to tmux is Ctrl+b
.
You will see a notification message that says 'tmux environment saved' at the status bar.
1.4. Restoring Tmux Environment
You can restore a closed tmux environment at any time by pressing the Prefix + Ctrl-r
key combination.
To verify that tmux-resurrect is working, simply close or detach from a Tmux session. Then, open a new Tmux session and press Prefix + Ctrl-s
. The saved Tmux session will open.
Please note that you need to manually save and restore the Tmux environment. If you wish to automate this process, you can use the tmux-continuum
plugin as described in the following section.
2. Automatically Save and Restore Tmux Sessions with Tmux-continuum
2.1. What is Tmux-continuum?
Tmux-continuum is another Tmux plugin that works in conjunction with tmux-resurrect
to automatically save and restore sessions. It provides more automated session management.
2.2. Install Tmux-continuum
It's recommended to use Tmux Plugin Manager (TPM) to install the tmux-continuum plugin. Before proceeding, make sure you have TPM installed, and then use TPM to install the tmux-resurrect plugin as well.
Add the tmux-continuum plugin to your list of TPM plugins in your .tmux.conf
:
set -g @plugin 'tmux-plugins/tmux-resurrect' set -g @plugin 'tmux-plugins/tmux-continuum'
Use the prefix key (prefix + I
) to fetch the plugin and source it. Once activated, the plugin will work silently in the background without requiring further action, continuously saving your Tmux environment.
Heads Up: the Tmux-continuum plugin tries to run periodically by updating the status-right Tmux variable. However, if another plugin, usually themes, changes the status-right variable, the autosave feature stops working. To resolve this problem, simply move the Tmux-continuum plugin to the end of your list of TPM plugins. This is a known issue with the Tmux-continuum plugin. Hope it will be resolved in the future versions.
2.3. Automatic Save
This plugin continuously saves your Tmux environment in the background at regular intervals (every 15 minutes). It ensures that your Tmux setup is always up to date, preserving your sessions, windows, and panes.
2.4. Automatic Restore
This plugin also provides automatic restoration of your last saved Tmux environment whenever you start Tmux. This means that no matter what happens, if your machine is running, Tmux will be there in the state you left it during your last session.
To enable this feature, simply add set -g @continuum-restore 'on'
to your .tmux.conf
file.
It's worth noting that the automatic restore feature exclusively triggers when you start the Tmux server. Other actions, such as sourcing .tmux.conf
, won't trigger this behavior.
Related Read:
- How To Autostart Tmux Session On Remote System When Logging In Via SSH In Linux
- Enhancing SSH Login With A Tmux Session Selection Menu In Linux
Frequently Asked Questions (FAQ)
A: A Tmux environment refers to the setup of your Tmux sessions, windows, panes, and their configurations. It also includes the state of running programs and the layout of your Tmux workspace.
A: Saving and restoring your Tmux environment allows you to retain your workspace, active programs, and configurations even after system reboots or when you close your terminal. It's a convenient way to pick up where you left off.
A: tmux-resurrect: This plugin allows you to manually save and restore your Tmux environment. It provides flexibility and control over when to save and restore.
tmux-continuum: This plugin offers continuous and automated saving and restoration of your Tmux environment. It ensures your Tmux session is always up to date and starts Tmux automatically when your system boots.
A: No, these plugins are designed to be easy to use. You may need to add them to your Tmux configuration file and enable specific features, but they generally work with minimal setup.
A: You'll typically need Tmux version 1.9 or higher, Bash, and the tmux-resurrect plugin as a dependency to use tmux-continuum.
A: By default, the Tmux environment is saved every 15 minutes with tmux-continuum. You can typically adjust this interval to suit your preferences.
A: Yes, both tmux-resurrect and tmux-continuum are tested and compatible with Linux, OSX, and Cygwin.
Conclusion
There you have it. The tmux-resurrect and tmux-continuum plugins can help you achieve persistent Tmux sessions across system reboots by saving and restoring your Tmux environment. Choose any one of the plugins that best suits your needs and workflow.
I use both plugins in my Tmux environment, and I never worry about accidental terminal closures or system reboots.
Resources: