Skip to content

Instantly share code, notes, and snippets.

@jyurek
Last active November 14, 2024 16:26
Show Gist options
  • Save jyurek/7be666a88e06f68d45cf to your computer and use it in GitHub Desktop.
Save jyurek/7be666a88e06f68d45cf to your computer and use it in GitHub Desktop.
Create and switch sessions in tmux quickly
#!/bin/sh
tm() {
if [ -z $1 ]; then
tmux switch-client -l
else
if [ -z "$TMUX" ]; then
tmux new-session -As $1
else
if ! tmux has-session -t $1 2>/dev/null; then
TMUX= tmux new-session -ds $1
fi
tmux switch-client -t $1
fi
fi
}
tm $1
# tmux session tab complete function
_tmux_complete_session() {
local IFS=$'\n'
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$(tmux -q list-sessions | cut -f 1 -d ':')" -- "${cur}") )
}
complete -F _tmux_complete_session tm
# Toggle between the last two sessions
bind m switch-client -l
bind M command-prompt -p 'switch session:' "run \"tm '%%'\""
@calebhearth
Copy link

Jon do you think it's time to move this to a project with a proper homebrew package and stuff?

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