Skip to content

Instantly share code, notes, and snippets.

@fsgreco
Last active April 1, 2024 17:20
Show Gist options
  • Save fsgreco/be959f9cc43600db8d9ea50941f16f03 to your computer and use it in GitHub Desktop.
Save fsgreco/be959f9cc43600db8d9ea50941f16f03 to your computer and use it in GitHub Desktop.
[zsh function] a more advanced tmux sessionizer it can take just a name (-n the_name) optionally
function tm() {
just_name=
while getopts "n:" opt; do
case $opt in
n) just_name="$OPTARG" ;;
esac
done
# if you run `tm .` the directory name will be the name of the session
if [ "$1" = "." ] ; then just_name="$(basename "$PWD" | tr . _)"; fi
# if you run the function with `-n the_name` arguments will be these ones:
[ -n "$just_name" ] && args=( new -s "$just_name" -c $(pwd) )
# Otherwise this will be the default behaviour (you will ask to select the initial folder)
if [ -z "$just_name" ]; then
selected=$(find ~/Desktop/SANDBOX ~/ ~/Desktop/TEMP -mindepth 1 -maxdepth 1 -type d | fzf)
selected_name=$(basename "$selected" | tr . _)
args=( new -s $selected_name -c $selected)
fi
tmux_running=$(pgrep tmux)
# If there is no tmux server running create the session
[ ! $tmux_running ] && tmux "${args[@]}" && return 0
# There is at least a tmux server running
# If you're not inside tmux either attach to the session or create it:
[ -z "$TMUX" ] && args+=(-A) && tmux "${args[@]}" && return 0
# You're already inside tmux => [ -n "$TMUX" ] should be true
# If session doesn't exist create it and then switch to it
session_name=${just_name:-$selected_name}
if ! tmux has -t $session_name > /dev/null 2>&1; then
args+=(-d)
tmux "${args[@]}"
fi
args=(switchc -t $session_name)
tmux "${args[@]}"
}
@invisibleaxm
Copy link

i just finished watching the same classs and saw your PR which let me to this gist, just wanted to say ty it works great!

@fsgreco
Copy link
Author

fsgreco commented May 30, 2023

possible typo at args=(switchc -t $session_name) => "switchC"?

Sorry for the late response @TheJoeSchr , It's not a typo, switchc should "create and switch to it"

@fsgreco
Copy link
Author

fsgreco commented May 30, 2023

i just finished watching the same classs and saw your PR which let me to this gist, just wanted to say ty it works great!

I'm glad it's useful for you! Thanks for the comment :)

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