-
-
Save mikedh/f3528dfbed78c7537541b9c0aca1620f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| sux - a less annoying tmux wrapper | |
| Usage: | |
| sux Attach to most recent session, or create new | |
| sux -l, --list List sessions | |
| sux <name> Attach to session, create if missing | |
| sux --config Write sane tmux config to ~/.tmux.conf | |
| """ | |
| import argparse | |
| import os | |
| import shutil | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| TMUX_CONFIG = """\ | |
| # Enable mouse mode (scroll, click, resize panes) | |
| set -g mouse on | |
| # Increase scrollback buffer | |
| set -g history-limit 50000 | |
| # Start window/pane numbering at 1 | |
| set -g base-index 1 | |
| setw -g pane-base-index 1 | |
| # Faster escape time (helps with vim) | |
| set -sg escape-time 10 | |
| # Better colors | |
| set -g default-terminal "screen-256color" | |
| # Ctrl-a prefix (screen-like) | |
| set -g prefix C-a | |
| unbind C-b | |
| bind C-a send-prefix | |
| bind C-d detach | |
| """ | |
| def run_tmux(*args): | |
| """Run tmux command, replacing current process.""" | |
| os.execvp("tmux", ["tmux"] + list(args)) | |
| def tmux_running(): | |
| """Check if tmux server is running.""" | |
| return subprocess.run( | |
| ["tmux", "list-sessions"], | |
| capture_output=True | |
| ).returncode == 0 | |
| def apply_config(): | |
| """Write tmux config to ~/.tmux.conf""" | |
| conf_path = Path.home() / ".tmux.conf" | |
| backup_path = Path.home() / ".tmux.conf.bak" | |
| if conf_path.exists(): | |
| shutil.copy(conf_path, backup_path) | |
| print(f"Backed up existing config to {backup_path}") | |
| conf_path.write_text(TMUX_CONFIG) | |
| print(f"Wrote config to {conf_path}") | |
| if tmux_running(): | |
| subprocess.run(["tmux", "source-file", str(conf_path)]) | |
| print("Reloaded tmux config") | |
| def list_sessions(): | |
| """List tmux sessions.""" | |
| subprocess.run(["tmux", "list-sessions"]) | |
| def attach_or_create(name=None): | |
| """Attach to session, creating if needed.""" | |
| if name: | |
| # Try attach, fall back to new session with that name | |
| if tmux_running(): | |
| result = subprocess.run( | |
| ["tmux", "has-session", "-t", name], | |
| capture_output=True | |
| ) | |
| if result.returncode == 0: | |
| run_tmux("attach-session", "-t", name) | |
| else: | |
| run_tmux("new-session", "-s", name) | |
| else: | |
| run_tmux("new-session", "-s", name) | |
| else: | |
| # Attach to most recent, or create new | |
| if tmux_running(): | |
| run_tmux("attach-session") | |
| else: | |
| run_tmux("new-session") | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="A less annoying tmux wrapper", | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=__doc__ | |
| ) | |
| parser.add_argument( | |
| "-l", "--list", | |
| action="store_true", | |
| help="List sessions" | |
| ) | |
| parser.add_argument( | |
| "--config", | |
| action="store_true", | |
| help="Write sane tmux config to ~/.tmux.conf" | |
| ) | |
| parser.add_argument( | |
| "session", | |
| nargs="?", | |
| help="Session name to attach/create" | |
| ) | |
| args = parser.parse_args() | |
| if args.config: | |
| apply_config() | |
| elif args.list: | |
| list_sessions() | |
| else: | |
| attach_or_create(args.session) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment