summaryrefslogtreecommitdiff
path: root/plugins/tmux/tmux.plugin.zsh
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/tmux/tmux.plugin.zsh')
-rw-r--r--plugins/tmux/tmux.plugin.zsh103
1 files changed, 88 insertions, 15 deletions
diff --git a/plugins/tmux/tmux.plugin.zsh b/plugins/tmux/tmux.plugin.zsh
index 72cdd4818..b1d97e98f 100644
--- a/plugins/tmux/tmux.plugin.zsh
+++ b/plugins/tmux/tmux.plugin.zsh
@@ -13,18 +13,32 @@ fi
: ${ZSH_TMUX_AUTOCONNECT:=true}
# Automatically close the terminal when tmux exits
: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
+# Automatically name the new session based on the basename of PWD
+: ${ZSH_TMUX_AUTONAME_SESSION:=false}
+# Automatically pick up tmux environments
+: ${ZSH_TMUX_AUTOREFRESH:=false}
# Set term to screen or screen-256color based on current terminal support
+: ${ZSH_TMUX_DETACHED:=false}
+# Set detached mode
: ${ZSH_TMUX_FIXTERM:=true}
# Set '-CC' option for iTerm2 tmux integration
: ${ZSH_TMUX_ITERM2:=false}
# The TERM to use for non-256 color terminals.
-# Tmux states this should be screen, but you may need to change it on
+# Tmux states this should be tmux|screen, but you may need to change it on
# systems without the proper terminfo
-: ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
+if [[ -e /usr/share/terminfo/t/tmux ]]; then
+ : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=tmux}
+else
+ : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
+fi
# The TERM to use for 256 color terminals.
-# Tmux states this should be screen-256color, but you may need to change it on
+# Tmux states this should be (tmux|screen)-256color, but you may need to change it on
# systems without the proper terminfo
-: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
+if [[ -e /usr/share/terminfo/t/tmux-256color ]]; then
+ : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=tmux-256color}
+else
+ : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
+fi
# Set the configuration path
if [[ -e $HOME/.tmux.conf ]]; then
: ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
@@ -37,14 +51,42 @@ fi
: ${ZSH_TMUX_UNICODE:=false}
# ALIASES
-alias ta='tmux attach -t'
-alias tad='tmux attach -d -t'
-alias ts='tmux new-session -s'
-alias tl='tmux list-sessions'
+function _build_tmux_alias {
+ setopt localoptions no_rc_expand_param
+ eval "function $1 {
+ if [[ -z \$1 ]] || [[ \${1:0:1} == '-' ]]; then
+ tmux $2 \"\$@\"
+ else
+ tmux $2 $3 \"\$@\"
+ fi
+ }"
+
+ local f s
+ f="_omz_tmux_alias_${1}"
+ s=(${(z)2})
+
+ eval "function ${f}() {
+ shift words;
+ words=(tmux ${@:2} \$words);
+ ((CURRENT+=${#s[@]}+1))
+ _tmux
+ }"
+
+ compdef "$f" "$1"
+}
+
alias tksv='tmux kill-server'
-alias tkss='tmux kill-session -t'
+alias tl='tmux list-sessions'
alias tmuxconf='$EDITOR $ZSH_TMUX_CONFIG'
+_build_tmux_alias "ta" "attach" "-t"
+_build_tmux_alias "tad" "attach -d" "-t"
+_build_tmux_alias "to" "new-session -A" "-s"
+_build_tmux_alias "ts" "new-session" "-s"
+_build_tmux_alias "tkss" "kill-session" "-t"
+
+unfunction _build_tmux_alias
+
# Determine if the terminal supports 256 colors
if [[ $terminfo[colors] == 256 ]]; then
export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
@@ -77,11 +119,26 @@ function _zsh_tmux_plugin_run() {
[[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
[[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
+ local _detached=""
+ [[ "$ZSH_TMUX_DETACHED" == "true" ]] && _detached="-d"
+
+ local session_name
+ if [[ "$ZSH_TMUX_AUTONAME_SESSION" == "true" ]]; then
+ # Name the session after the basename of the current directory
+ session_name=${PWD##*/}
+ # If the current directory is the home directory, name it 'HOME'
+ [[ "$PWD" == "$HOME" ]] && session_name="HOME"
+ # If the current directory is the root directory, name it 'ROOT'
+ [[ "$PWD" == "/" ]] && session_name="ROOT"
+ else
+ session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME"
+ fi
+
# Try to connect to an existing session.
- if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
- [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach -t $ZSH_TMUX_DEFAULT_SESSION_NAME
+ if [[ -n "$session_name" ]]; then
+ [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
else
- [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
+ [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached
fi
# If failed, just run tmux, fixing the TERM variable if requested.
@@ -91,8 +148,9 @@ function _zsh_tmux_plugin_run() {
elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
fi
- if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
- $tmux_cmd new-session -s $ZSH_TMUX_DEFAULT_SESSION_NAME
+
+ if [[ -n "$session_name" ]]; then
+ $tmux_cmd new-session -s "$session_name"
else
$tmux_cmd new-session
fi
@@ -103,6 +161,15 @@ function _zsh_tmux_plugin_run() {
fi
}
+# Refresh tmux environment variables.
+function _zsh_tmux_plugin_preexec()
+{
+ local -a tmux_cmd
+ tmux_cmd=(command tmux)
+
+ eval $($tmux_cmd show-environment -s)
+}
+
# Use the completions for tmux for our function
compdef _tmux _zsh_tmux_plugin_run
# Alias tmux to our wrapper function.
@@ -122,10 +189,16 @@ function _tmux_directory_session() {
alias tds=_tmux_directory_session
# Autostart if not already in tmux and enabled.
-if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" ]]; then
+if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" && -z "$ZED_TERM" ]]; then
# Actually don't autostart if we already did and multiple autostarts are disabled.
if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]; then
export ZSH_TMUX_AUTOSTARTED=true
_zsh_tmux_plugin_run
fi
fi
+
+# Automatically refresh tmux environments if tmux is running.
+if [[ -n "$TMUX" && "$ZSH_TMUX_AUTOREFRESH" == "true" ]] && tmux ls >/dev/null 2>/dev/null; then
+ autoload -U add-zsh-hook
+ add-zsh-hook preexec _zsh_tmux_plugin_preexec
+fi