Skip to content

Instantly share code, notes, and snippets.

@HackingGate
Last active April 5, 2026 05:28
Show Gist options
  • Select an option

  • Save HackingGate/ebc5bd4d474d6aaffa05bfb84cc1e60f to your computer and use it in GitHub Desktop.

Select an option

Save HackingGate/ebc5bd4d474d6aaffa05bfb84cc1e60f to your computer and use it in GitHub Desktop.
Emacs-friendly tmux setup with clipboard copy, auto light/dark mode, and longer message display time
#!/usr/bin/env bash
set -euo pipefail
echo "Starting tmux and shell environment setup..."
# Detect OS/session and configure clipboard behavior
clip_cmd=""
copy_target="tmux OSC 52 host clipboard"
os_name="$(uname -s 2>/dev/null || true)"
is_wayland=0
is_ssh=0
has_display=0
if [[ "${XDG_SESSION_TYPE:-}" == "wayland" ]] || [[ -n "${WAYLAND_DISPLAY:-}" ]]; then
is_wayland=1
fi
if [[ -n "${SSH_CONNECTION:-}" ]] || [[ -n "${SSH_CLIENT:-}" ]]; then
is_ssh=1
fi
if [[ -n "${DISPLAY:-}" ]] || [[ -n "${WAYLAND_DISPLAY:-}" ]]; then
has_display=1
fi
# For SSH sessions, prefer tmux's OSC 52 clipboard path so copied text lands on
# the local machine connected to the SSH client instead of the remote host.
use_tmux_clipboard=1
if [[ "${is_ssh}" -eq 0 ]] && [[ "${has_display}" -eq 1 ]]; then
use_tmux_clipboard=0
fi
case "${os_name}" in
Linux)
if [[ "${use_tmux_clipboard}" -eq 0 ]]; then
if [[ "${is_wayland}" -eq 1 ]]; then
if ! command -v wl-copy >/dev/null 2>&1; then
if command -v apt >/dev/null 2>&1; then
sudo apt update >/dev/null 2>&1
sudo apt install -y wl-clipboard >/dev/null 2>&1
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y wl-clipboard >/dev/null 2>&1
fi
fi
if command -v wl-copy >/dev/null 2>&1; then
clip_cmd="wl-copy"
fi
else
if ! command -v xclip >/dev/null 2>&1; then
if command -v apt >/dev/null 2>&1; then
sudo apt update >/dev/null 2>&1
sudo apt install -y xclip >/dev/null 2>&1
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y xclip >/dev/null 2>&1
fi
fi
if command -v xclip >/dev/null 2>&1; then
clip_cmd="xclip -in -selection clipboard"
fi
fi
fi
;;
Darwin)
if [[ "${use_tmux_clipboard}" -eq 0 ]]; then
clip_cmd="pbcopy"
fi
;;
esac
# Define the final copy action
if [[ -n "${clip_cmd}" ]]; then
copy_action="copy-pipe-and-cancel \"${clip_cmd}\""
copy_target="local desktop clipboard helper (${clip_cmd})"
else
copy_action="copy-selection-and-cancel"
fi
# Create tmux configuration
cat > "${HOME}/.tmux.conf" <<EOF
# --- 1. Colors & Terminal ---
set -g default-terminal "screen-256color"
set -as terminal-overrides ',xterm*:Tc:smcup@:rmcup@'
set -as terminal-features ',xterm*:clipboard'
set -s set-clipboard on
set -g allow-passthrough on
set -g focus-events on
set -ag update-environment "DISPLAY WAYLAND_DISPLAY XAUTHORITY SSH_AUTH_SOCK SSH_CONNECTION SSH_TTY XDG_SESSION_TYPE"
# --- 2. Visibility & History ---
set -g mouse on
set -g history-limit 1000000
# High-visibility highlight (Bright Cyan)
set -g mode-style "bg=brightcyan,fg=black,bold"
# --- 3. Keybindings ---
set -g mode-keys emacs
setw -g mode-keys emacs
set -g status-keys emacs
# --- 4. System Clipboard & Mouse Selection ---
bind -n MouseDrag1Pane if-shell -F "#{mouse_any_flag}" "send-keys -M" "copy-mode -M"
bind -T copy-mode-emacs MouseDragEnd1Pane send-keys -X ${copy_action}
bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X ${copy_action}
# Copy keys
bind -T copy-mode-emacs Enter send-keys -X ${copy_action}
bind -T copy-mode-emacs M-w send-keys -X ${copy_action}
bind -T copy-mode-emacs C-w send-keys -X ${copy_action}
bind -T copy-mode-vi Enter send-keys -X ${copy_action}
# Selection starting keys
unbind -T copy-mode-emacs C-@
bind -T copy-mode-emacs C-@ send-keys -X begin-selection
# Optional: double-click word, triple-click line
bind -T copy-mode-emacs DoubleClick1Pane select-pane \; send-keys -X select-word \; send-keys -X ${copy_action}
bind -T copy-mode-emacs TripleClick1Pane select-pane \; send-keys -X select-line \; send-keys -X ${copy_action}
bind -T copy-mode-vi DoubleClick1Pane select-pane \; send-keys -X select-word \; send-keys -X ${copy_action}
bind -T copy-mode-vi TripleClick1Pane select-pane \; send-keys -X select-line \; send-keys -X ${copy_action}
# --- 5. Quality of Life ---
set -g base-index 1
setw -g pane-base-index 1
set -g display-time 4000
bind r source-file ~/.tmux.conf \; display-message "Tmux config reloaded!"
EOF
# --- Force apply tmux changes to ALL sessions ---
if command -v tmux >/dev/null 2>&1 && tmux has-session >/dev/null 2>&1; then
tmux list-sessions -F '#S' > /tmp/tmux_sessions.txt
while read -r s; do
tmux set-option -t "$s" -g mode-keys emacs >/dev/null 2>&1 || true
tmux set-window-option -t "$s" -g mode-keys emacs >/dev/null 2>&1 || true
tmux source-file -t "$s" "${HOME}/.tmux.conf" >/dev/null 2>&1 || true
done < /tmp/tmux_sessions.txt
rm /tmp/tmux_sessions.txt
fi
echo "---"
echo "=== HOW TO COPY (Shift+Number Compatible) ==="
echo ""
echo "[Method A: With Mouse / Trackpad]"
echo " • Click & Drag -> Release to auto-copy."
echo ""
echo "[Method B: Keyboard Selection]"
echo " 1. Enter Copy Mode: Ctrl+b ["
echo " 2. Start Selection: Press 'Ctrl+@'"
echo " 3. Copy & Exit: Press 'Enter' (or 'Alt+w' / 'Ctrl+w')"
echo ""
echo "[Method C: Bypass Tmux]"
echo " • Hold SHIFT while dragging with the mouse."
echo "==================="
echo "Reload all sessions: Press Ctrl+b, then 'r'"
@HackingGate
Copy link
Copy Markdown
Author

HackingGate commented Mar 25, 2026

bash <(curl -fsSL https://gist.githubusercontent.com/HackingGate/ebc5bd4d474d6aaffa05bfb84cc1e60f/raw/tmux.sh)

Clipboard copy, automatic light/dark theme switching, and a longer tmux message display time are included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment