Tmux For Session Sharing And Process Reattachment
Getting Started with Tmux
Installing and launching tmux
Tmux is a terminal multiplexer that allows multiple terminal sessions to be accessed simultaneously in a single window. To get started with tmux, first install it on your system with your package manager of choice.
On Linux:
$ sudo apt install tmux #Ubuntu/Debian $ sudo yum install tmux #RHEL/CentOS $ sudo dnf install tmux #Fedora
Once installed, simply type tmux
in your terminal to start a new tmux session. You will notice your terminal prompt is now prefixed with tmux
, indicating you are inside a tmux session.
Understanding tmux concepts – sessions, windows, panes
Tmux provides several abstractions for managing multiple processes:
- Sessions – An overarching tmux session that can contain multiple windows.
- Windows – A single viewport onto one or more panes. You can have multiple windows in a session.
- Panes – A single terminal instance within a window. Panes can run shell prompts, text editors, etc.
Conceptually, a session is a collection of windows, while each window divides itself into one or more panes. You can easily create, delete, rearrange, and navigate these components.
Basic tmux commands – new session, split pane, switch pane
Here are some basic tmux commands to get started:
# Start new session tmux new -s mysession # Split pane horizontally Ctrl+b " # Split pane vertically Ctrl+b % # Switch to next pane Ctrl+b o # Switch to session "mysession" tmux attach -t mysession # Detach from session Ctrl+b d
This allows you to start making use of tmux’s session, window, and pane abstractions.
Sharing Tmux Sessions
Enabling other users to attach
By default, tmux sessions can only be accessed by the user who started them. To enable other users to attach:
# Allow other users to read/write tmux set -t mysession acl-add user123 -w # Allow other users read-only access tmux set -t mysession acl-add user456 -r
Now user123 can attach to “mysession” with full access, while user456 has read-only access.
Detaching and reattaching to sessions
To detach from a tmux session:
Ctrl+b d
This keeps the tmux session running in the background.
To reattach later:
tmux attach -t mysession
This lets you resume right where you left off in “mysession”.
Read-only access for collaboration
You can grant other users read-only access to a session for collaborating:
tmux set -t mysession acl-add user456 -r
Now when user456 attaches to “mysession”, they will not be able to send keystrokes/commands. This allows them to observe without disrupting the session.
Great for pair programming, mentoring, etc!
Tmux Persistence
Keeping sessions running after detach
Tmux keeps sessions persistent by default when detached. This preserves all programs running within.
To explicitly configure this:
# In ~/.tmux.conf set -g detach-on-destroy off
Now tmux sessions will continue running in the background when detached.
Reattaching to previous sessions
If tmux is closed improperly or crashes, you can resume prior sessions with:
tmux attach -t mysession
This reattaches to the persistent “mysession” in the background.
Session names for easy reattachment
Use descriptive session names like “dev” or “docs” for convenience:
tmux new -s dev tmux new -s docs
You can now easily resume either session via name:
tmux attach -t dev tmux attach -t docs
Advanced Tmux Usage
Customizing status bar and colors
Tmux offers extensive customization via ~/.tmux.conf. For example, to configure the status bar:
# In ~/.tmux.conf # Status bar colors set -g status-bg black set -g status-fg white # Left aligned info set -g status-left "#S [#I:#P]" # Window numbering set -g status-right "#(tmux-mem-cpu-load)#W" # Activity monitoring setw -g monitor-activity on set -g visual-activity on
There are many color, style, and information options available for the status bar.
Scripting and automation with tmux
Commands can be run when certain tmux conditions occur by binding scripts to hooks:
# Runs on session create set -g status-right "#(tmux-mem-cpu-load)#W" # Run script when window closed set-hook -g session-destroy-hook '/home/user123/tmux-cleanup.sh' # Bind key to script bind R run '~/reload-config.sh'
This allows extensive tmux automation via shell scripts.
Integrating with shell and text editors
Tmux works well with other terminals like vim/neovim. For example:
# Vim/Tmux integration set -g status-right "#(tmux-mem-cpu-load)#W" # Mouse support in vim set -g mouse on # Easy switching between vim and tmux panes bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R
Similar integrations are available for shells like zsh too.
Tmux Tips and Tricks
Scrollback, copy mode, and search
Tmux allows scrolling through previous terminal output via copy mode:
# Enter copy mode Ctrl+b [ # Scroll up/down with Vi keys Up, Down # Search output Ctrl+f # Copy highlighted text Ctrl+Spacebar
This lets you revisit old content printed in tmux.
Layouts and pane arrangements
Tmux offers many preset pane layouts:
# Split 4 panes evenly Ctrl+b Meta+1 # 2 panes top and bottom Ctrl+b Meta+2 # 2 panes side by side Ctrl+b Meta+3 # Cycle layouts Ctrl+b Spacebar
You can create custom layouts as well in ~/.tmux.conf for added flexibility.
Debugging and troubleshooting FAQs
Some tips for resolving issues:
- Use
tmux info
to diagnose session/server issues - Check ~/.tmux.conf for incorrect syntax or bindings
- Disable plugins and customizations to isolate problems
- See status bar for warnings about unsupported features
- Search logs at /var/log/tmux.log for error messages
The tmux mailing list at [email protected] is also a great resource!