7091597c138e9327088f1df7e87733cc22e352a8
created, updated, author
| created | updated | author |
|---|---|---|
| 2015-09-09 | 2026-07-21 | chawley |
#readme
TMUX Hack Session Launcher
A Bash script designed to manage and automate pre-defined tmux-sessions. This script allows you to launch complex development or administration environments - complete with multiple windows and SSH connections - using a single command.
Features
- Automated Session Creation: Checks if a session exists; if not, it creates it with your pre-defined windows and layouts.
- Environment Nesting Protection: Prevents launching a new session if you are already inside an active tmux environment.
- Custom Configuration Support: Automatically sources your
$HOME/.tmux.conf. - Session Attaching: If a requested session is already running, the script simply attaches you to it.
- Fallback Discovery: Provides an error message and lists all currently active sessions if an invalid name is provided.
Prerequisites
- tmux: Ensure
tmuxis installed and available in your$PATH. - SSH Config (Optional): For windows that execute SSH commands, ensure your SSH keys or config are set up for seamless connections.
Installation
-
Copy the script to a directory in your
$PATH(e.g.,~/binor/usr/local/bin). -
Rename it to something short, like
tm. -
Make the script executable:
chmod +x ~/bin/tm
Usage
Run the script followed by the name of the pre-defined session:
tm mine
If no session name is provided, or the name doesn't match a case in the script, it will display an error and list all currently active tmux sessions.
Configuration
To add a new session, add a new case stanza inside the case $session in block.
Template
session_name)
if [[ $tcheck == 0 ]]; then
tmux -f "${TMUX_CONF}" new-session -d -s session_name
tmux -f "${TMUX_CONF}" rename-window 'initial_window'
tmux -f "${TMUX_CONF}" new-window -t:1 -n 'second_window' 'command_to_run'
fi
tmux attach-session -t session_name
;;
Script Variables
TMUX_CONF: Defaults to$HOME/.tmux.conf. Modify this variable if your configuration file is located elsewhere.
Tmux Menu Improved (TMI)
A vibe-coded improvement on the original
Key Improvements & Refactor Notes
- Robust Session Detection (
tmux has-session): Replacedtmux list-sessions | grep -cwithtmux has-session -t "=${session}". Using exact match targeting (=) prevents substring false positives (e.g., matchingmineagainstmine-devor log strings) and eliminates pipe subshell overhead. - Declarative Data Structure: Session definitions are separated from the initialization logic using an associative array (
SESSIONS). Adding a new layout only requires defining a new key and window matrix, removing repetitivetmux new-windowboilerplates. - Process Handover via
exec: Replaced subshell execution oftmux attach-sessionwithexec tmux .... This replaces the bash script process with the tmux client process directly, eliminating an orphaned shell process remaining in memory while attached. - Strict Shell Hygiene: Added
set -euo pipefailand proper variable quoting to prevent unhandled failures or field splitting errors. redirected operational error output tostderr(>&2). - Interactive Fallback (
fzf): If invoked without arguments (tm), the script checks forfzfin$PATH. If present, it serves an interactive fuzzy selector of configured session keys rather than instantly printing an error.
Languages
Shell
100%