Last active
April 1, 2024 17:20
-
-
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
This file contains 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
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[@]}" | |
} |
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!
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"
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
possible typo at
args=(switchc -t $session_name)
=> "switchC"?