Parent directory

tmux.plugin.zsh

6583 bytes
  1if ! (( $+commands[tmux] )); then
  2  print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." >&2
  3  return 1
  4fi
  5
  6# CONFIGURATION VARIABLES
  7# Automatically start tmux
  8: ${ZSH_TMUX_AUTOSTART:=false}
  9# Only autostart once. If set to false, tmux will attempt to
 10# autostart every time your zsh configs are reloaded.
 11: ${ZSH_TMUX_AUTOSTART_ONCE:=true}
 12# Automatically connect to a previous session if it exists
 13: ${ZSH_TMUX_AUTOCONNECT:=true}
 14# Automatically close the terminal when tmux exits
 15: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
 16# Automatically name the new session based on the basename of PWD
 17: ${ZSH_TMUX_AUTONAME_SESSION:=false}
 18# Automatically pick up tmux environments
 19: ${ZSH_TMUX_AUTOREFRESH:=false}
 20# Set term to screen or screen-256color based on current terminal support
 21: ${ZSH_TMUX_DETACHED:=false}
 22# Set detached mode
 23: ${ZSH_TMUX_FIXTERM:=true}
 24# Set '-CC' option for iTerm2 tmux integration
 25: ${ZSH_TMUX_ITERM2:=false}
 26# The TERM to use for non-256 color terminals.
 27# Tmux states this should be tmux|screen, but you may need to change it on
 28# systems without the proper terminfo
 29if [[ -e /usr/share/terminfo/t/tmux ]]; then
 30  : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=tmux}
 31else
 32  : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
 33fi
 34# The TERM to use for 256 color terminals.
 35# Tmux states this should be (tmux|screen)-256color, but you may need to change it on
 36# systems without the proper terminfo
 37if [[ -e /usr/share/terminfo/t/tmux-256color ]]; then
 38  : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=tmux-256color}
 39else
 40  : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
 41fi
 42# Set the configuration path
 43if [[ -e $HOME/.tmux.conf ]]; then
 44  : ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
 45elif [[ -e ${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf ]]; then
 46  : ${ZSH_TMUX_CONFIG:=${XDG_CONFIG_HOME:-$HOME/.config}/tmux/tmux.conf}
 47else
 48  : ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
 49fi
 50# Set -u option to support unicode
 51: ${ZSH_TMUX_UNICODE:=false}
 52
 53# ALIASES
 54function _build_tmux_alias {
 55  setopt localoptions no_rc_expand_param
 56  eval "function $1 {
 57    if [[ -z \$1 ]] || [[ \${1:0:1} == '-' ]]; then
 58      tmux $2 \"\$@\"
 59    else
 60      tmux $2 $3 \"\$@\"
 61    fi
 62  }"
 63
 64  local f s
 65  f="_omz_tmux_alias_${1}"
 66  s=(${(z)2})
 67
 68  eval "function ${f}() {
 69    shift words;
 70    words=(tmux ${@:2} \$words);
 71    ((CURRENT+=${#s[@]}+1))
 72    _tmux
 73  }"
 74
 75  compdef "$f" "$1"
 76}
 77
 78alias tksv='tmux kill-server'
 79alias tl='tmux list-sessions'
 80alias tmuxconf='$EDITOR $ZSH_TMUX_CONFIG'
 81
 82_build_tmux_alias "ta" "attach" "-t"
 83_build_tmux_alias "tad" "attach -d" "-t"
 84_build_tmux_alias "to" "new-session -A" "-s"
 85_build_tmux_alias "ts" "new-session" "-s"
 86_build_tmux_alias "tkss" "kill-session" "-t"
 87
 88unfunction _build_tmux_alias
 89
 90# Determine if the terminal supports at least 256 colors
 91if (( ${+terminfo[colors]} )) && [[ $terminfo[colors] -ge 256 ]]; then
 92  export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
 93else
 94  export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
 95fi
 96
 97# Handle $0 according to the standard:
 98# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
 990="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
1000="${${(M)0:#/*}:-$PWD/$0}"
101
102# Set the correct local config file to use.
103if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
104  export ZSH_TMUX_CONFIG
105  export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
106else
107  export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
108fi
109
110# Wrapper function for tmux.
111function _zsh_tmux_plugin_run() {
112  if [[ -n "$@" ]]; then
113    command tmux "$@"
114    return $?
115  fi
116
117  local -a tmux_cmd
118  tmux_cmd=(command tmux)
119  [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
120  [[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
121
122  local _detached=""
123  [[ "$ZSH_TMUX_DETACHED" == "true" ]] && _detached="-d"
124
125  local session_name
126  if [[ "$ZSH_TMUX_AUTONAME_SESSION" == "true" ]]; then
127    # Name the session after the basename of the current directory
128    session_name=${PWD##*/}
129    # If the current directory is the home directory, name it 'HOME'
130    [[ "$PWD" == "$HOME" ]] && session_name="HOME"
131    # If the current directory is the root directory, name it 'ROOT'
132    [[ "$PWD" == "/" ]] && session_name="ROOT"
133  else
134      session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME"
135  fi
136
137  # Try to connect to an existing session.
138  if [[ -n "$session_name" ]]; then
139    [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
140  else
141    [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached
142  fi
143
144  # If failed, just run tmux, fixing the TERM variable if requested.
145  if [[ $? -ne 0 ]]; then
146    if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]; then
147      tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
148    elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
149      tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
150    fi
151
152    if [[ -n "$session_name" ]]; then
153      $tmux_cmd new-session -s "$session_name"
154    else
155      $tmux_cmd new-session
156    fi
157  fi
158
159  if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
160    exit
161  fi
162}
163
164# Refresh tmux environment variables.
165function _zsh_tmux_plugin_preexec()
166{
167  local -a tmux_cmd
168  tmux_cmd=(command tmux)
169
170  eval "$($tmux_cmd show-environment -s)"
171}
172
173# Use the completions for tmux for our function
174compdef _tmux _zsh_tmux_plugin_run
175# Alias tmux to our wrapper function.
176alias tmux=_zsh_tmux_plugin_run
177
178function _tmux_directory_session() {
179  # current directory without leading path
180  local dir=${PWD##*/}
181  # md5 hash for the full working directory path
182  local md5=$(printf '%s' "$PWD" | md5sum | cut -d  ' ' -f 1)
183  # human friendly unique session name for this directory
184  local session_name="${dir}-${md5:0:6}"
185  # create or attach to the session
186  local -a tmux_cmd
187  tmux_cmd=(command tmux)
188  [[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
189  $tmux_cmd new -As "$session_name"
190}
191
192alias tds=_tmux_directory_session
193
194# Autostart if not already in tmux and enabled.
195if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" && -z "$ZED_TERM" ]]; then
196  # Actually don't autostart if we already did and multiple autostarts are disabled.
197  if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]; then
198    export ZSH_TMUX_AUTOSTARTED=true
199    _zsh_tmux_plugin_run
200  fi
201fi
202
203# Automatically refresh tmux environments if tmux is running.
204if [[ -n "$TMUX" && "$ZSH_TMUX_AUTOREFRESH" == "true" ]] && tmux ls >/dev/null 2>/dev/null; then
205  autoload -U add-zsh-hook
206  add-zsh-hook preexec _zsh_tmux_plugin_preexec
207fi