85 lines
3.5 KiB
Markdown
85 lines
3.5 KiB
Markdown
---
|
|
created: 2015-09-09
|
|
updated: 2026-07-21
|
|
author: 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 `tmux` is 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
|
|
|
|
1. Copy the script to a directory in your `$PATH` (e.g., `~/bin` or `/usr/local/bin`).
|
|
2. Rename it to something short, like `tm`.
|
|
3. Make the script executable:
|
|
|
|
```bash
|
|
chmod +x ~/bin/tm
|
|
```
|
|
|
|
---
|
|
|
|
## Usage
|
|
|
|
Run the script followed by the name of the pre-defined session:
|
|
|
|
```bash
|
|
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
|
|
```bash
|
|
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`):** Replaced `tmux list-sessions | grep -c` with `tmux has-session -t "=${session}"`. Using exact match targeting (`=`) prevents substring false positives (e.g., matching `mine` against `mine-dev` or 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 repetitive `tmux new-window` boilerplates.
|
|
- **Process Handover via `exec`:** Replaced subshell execution of `tmux attach-session` with `exec 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 pipefail` and proper variable quoting to prevent unhandled failures or field splitting errors. redirected operational error output to `stderr` (`>&2`).
|
|
- **Interactive Fallback (`fzf`):** If invoked without arguments (`tm`), the script checks for `fzf` in `$PATH`. If present, it serves an interactive fuzzy selector of configured session keys rather than instantly printing an error.
|