Skip to content

Instantly share code, notes, and snippets.

@CodeIter
Last active June 4, 2024 18:22
Show Gist options
  • Save CodeIter/33bc0bbb03c2b2d3f0b8a52bc8b7e848 to your computer and use it in GitHub Desktop.
Save CodeIter/33bc0bbb03c2b2d3f0b8a52bc8b7e848 to your computer and use it in GitHub Desktop.
Tmux Launcher - Session & Window Management. Effortlessly manage tmux sessions and open new windows with flexibility. This script checks for existing sessions, allows opening new ones in named groups, or attaches to the last session if preferred. Customize behavior with commented-out options.

Tmux Launcher - Session & Window Management

Effortlessly manage tmux sessions and open new windows with flexibility.

This script automates launching and managing tmux sessions and windows, offering flexibility and convenience.

Features

  • Automatic session creation: Identifies existing "main" session or creates a new one named "main".
  • Named session groups: Launch new sessions within grouped windows for better organization.
  • Flexible window opening: Select between opening a new session or attaching to the last one.
  • Commented-out options: Customize default behavior for advanced users.

Requirements

  • tmux installed on your system.

Usage

  1. Save the script as tmux-launcher.bash.
  2. Make it executable: chmod -c u+x tmux-launcher.bash.
  3. Place it in your $PATH directory.
  4. Source tmux-launcher.bash from your .bashrc.
curl -fSLROJ 'https://github.com/CodeIter/tmux-launch.bash/raw/main/tmux-launch.bash'
chmod -c u+x tmux-launcher.bash
mkdir -vp ~/.local/bin
mv -fv tmux-launcher.bash ~/.local/bin
echo -e '\nsource ~/.local/bin/tmux-launcher.bash\n' >> ~/.bashrc

Behavior

  • A main session possible names in this order: "main" "default" "master" "0".
  • A session group is named: <session_name>_grp
  • If a main session exists, opens a new window within its corresponding group.
  • If no main session exists, creates a new session named "main" and opens a new window.
  • You can customize behavior by uncommenting relevant sections in the script.

Advanced Options

  • Uncomment the _tmux_win_nbre section to open a new window in the existing "main" session.
  • Uncomment the tmux list-sessions section to attach to the last session instead of opening a new one.

Note

This script setup GnuPG agent by setting export GPG_TTY=$(tty) to enable gpg 's pine try inside Tmux. See Solved Arch forum thread pinentry password prompt broken inside tmux sessions Solution at post

Additional Information

  • This is a basic implementation with room for customization.
  • Refer to the script comments for further configuration options.
  • Feel free to contribute modifications or improvements via pull requests.

Enjoy streamlined tmux session management with this launcher!

License

This project is licensed under the terms of the MIT License.

MIT License
Copyright (c) 2024 Muhammad Amin Boubaker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

See tmux-launch.bash and README.md

#!/usr/bin/env bash
# Setup GnuPG agent
# Set GPG_TTY needed by tmux
# See https://bbs.archlinux.org/viewtopic.php?id=262296
export GPG_TTY=$(tty)
# Start tmux if installed
if [[ -z "${TMUX:-}" ]] \
&& [[ -z "${TMUX_PANE:-}" ]] \
; then
if type tmux &> /dev/null ; then
if [[ -z "${TMUX_MAIN_SESSION:-}" ]] ; then
for _i in main default master 0 ; do
if tmux has-session -t "${_i}" &> /dev/null ; then
export TMUX_MAIN_SESSION="${_i}"
if [[ "${_i}" == "0" ]] ; then
tmux rename-session t "${_i}" main \
&& export TMUX_MAIN_SESSION="main"
fi
break
fi
done
unset _i
fi
if [[ -n "${TMUX_MAIN_SESSION:-}" ]] ; then
_tmux_sess_group="${TMUX_MAIN_SESSION}_grp"
# Open new session
_tmux_sess_nbre="$(tmux list-sessions | wc -l)"
_tmux_sess_nbre="${_tmux_sess_nbre:-1}"
((_tmux_sess_nbre++))
_tmux_new_sess="${TMUX_MAIN_SESSION}${_tmux_sess_nbre}"
tmux new-session -d -s "${_tmux_new_sess}" -n main
exec tmux -l new-session -A -s "${_tmux_new_sess}" -t "${_tmux_sess_group}" &> /dev/null
# Or open new window in main (first) session
#_tmux_win_nbre="$(tmux list-window -t "${TMUX_MAIN_SESSION}" | wc -l)"
#_tmux_win_nbre="${_tmux_win_nbre:-1}"
#((_tmux_win_nbre++))
#_tmux_new_win="main${_tmux_win_nbre}"
#tmux new-session -A -d \
# -s "${TMUX_MAIN_SESSION}" \
# -n "${TMUX_MAIN_SESSION}" \; \
# new-window -a \
# -t "${TMUX_MAIN_SESSION}" \
# -n "${_tmux_new_win}" \; \
# detach \
# &> /dev/null
#exec tmux -l new-session -A \
# -s "${TMUX_MAIN_SESSION}" \
# -t "${_tmux_sess_group}" \
# &> /dev/null
else
# Open new session when there is no "main" session
export TMUX_MAIN_SESSION="main"
tmux new-session -d -s main -n main
exec tmux -l new-session -A -s main -t main_grp &> /dev/null
# Or open last session ,if not open new "main" session
#tmux list-sessions &> /dev/null \
#&& exec tmux -l attach-session \
#|| { tmux new-session -d -s main -n main ; exec tmux -l new-session -A -s main -t main_grp &> /dev/null ; }
fi
fi
fi
unset _tmux_new_win
unset _tmux_win_nbre
unset _tmux_sess_group
@CodeIter
Copy link
Author

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