From 5d5499ef9023672a4684ff8cd6e8881e186ef868 Mon Sep 17 00:00:00 2001 From: Michael Yockey Date: Fri, 14 Aug 2015 15:09:26 -0400 Subject: [PATCH] feat (tmux): Adding session management by convention --- modules/tmux/README.md | 2 ++ modules/tmux/functions/_tmux-new-session | 3 +++ .../functions/set-conventional-session-name | 9 +++++++ modules/tmux/functions/tmux-new-session | 24 +++++++++++++++++++ modules/tmux/init.zsh | 16 +++---------- 5 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 modules/tmux/functions/_tmux-new-session create mode 100644 modules/tmux/functions/set-conventional-session-name create mode 100644 modules/tmux/functions/tmux-new-session diff --git a/modules/tmux/README.md b/modules/tmux/README.md index 11c8a08a..41a6477d 100644 --- a/modules/tmux/README.md +++ b/modules/tmux/README.md @@ -44,6 +44,8 @@ Aliases - `tmuxa` attaches or switches to a tmux session. - `tmuxl` lists sessions managed by the tmux server. + - `tmuxn` creates or attaches to a session named for the passed argument or, + if non is provided, the current working directory. Caveats ------- diff --git a/modules/tmux/functions/_tmux-new-session b/modules/tmux/functions/_tmux-new-session new file mode 100644 index 00000000..2a94b247 --- /dev/null +++ b/modules/tmux/functions/_tmux-new-session @@ -0,0 +1,3 @@ +#compdef tmux-new-session + +compadd $(tmux list-sessions -F \#S | sed "s/[\* ]//g") diff --git a/modules/tmux/functions/set-conventional-session-name b/modules/tmux/functions/set-conventional-session-name new file mode 100644 index 00000000..9993e71d --- /dev/null +++ b/modules/tmux/functions/set-conventional-session-name @@ -0,0 +1,9 @@ +# +# Defines the naming convention for tmux-new-session +# +# Author: +# Michael Yockey +# + +TMUX_CONVENTIONAL_SESSION_NAME=$(basename $(pwd)) + diff --git a/modules/tmux/functions/tmux-new-session b/modules/tmux/functions/tmux-new-session new file mode 100644 index 00000000..b34867e8 --- /dev/null +++ b/modules/tmux/functions/tmux-new-session @@ -0,0 +1,24 @@ +# +# Creates or reattaches to a tmux session based on a naming convention +# +# Author: +# Michael Yockey +# + +local name + +if [[ -n $1 ]]; then + name=$1 +else + name=$TMUX_CONVENTIONAL_SESSION_NAME +fi + +tmux list-sessions | grep -q "^$name:" + +if [ $? = 0 ]; then + tmux attach-session -t $name +else + tmux new-session -s $name +fi + +return 0 diff --git a/modules/tmux/init.zsh b/modules/tmux/init.zsh index 9daf547a..535043f5 100644 --- a/modules/tmux/init.zsh +++ b/modules/tmux/init.zsh @@ -41,19 +41,9 @@ if [[ -z "$TMUX" && -z "$EMACS" && -z "$VIM" ]] && ( \ exec tmux $_tmux_iterm_integration attach-session fi -# -# Functions -# - -tmux-new-session() { - local name=$(basename $(pwd)) - tmux list-sessions | grep -q "^$name:" - if [ $? = 0 ]; then - tmux attach-session -t $name - else - tmux new-session -s $name - fi -} +# Initialize the session naming convention and update it before every command +set-conventional-session-name +preexec_functions=(${preexec_functions[@]} "set-conventional-session-name") # # Aliases