]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/tmux/tmux.plugin.zsh
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / tmux / tmux.plugin.zsh
1 if ! (( $+commands[tmux] )); then
2   print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." >&2
3   return 1
4 fi
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 # Set term to screen or screen-256color based on current terminal support
17 : ${ZSH_TMUX_FIXTERM:=true}
18 # Set '-CC' option for iTerm2 tmux integration
19 : ${ZSH_TMUX_ITERM2:=false}
20 # The TERM to use for non-256 color terminals.
21 # Tmux states this should be screen, but you may need to change it on
22 # systems without the proper terminfo
23 : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
24 # The TERM to use for 256 color terminals.
25 # Tmux states this should be screen-256color, but you may need to change it on
26 # systems without the proper terminfo
27 : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
28 # Set the configuration path
29 : ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
30 # Set -u option to support unicode
31 : ${ZSH_TMUX_UNICODE:=false}
32
33 # ALIASES
34
35 alias ta='tmux attach -t'
36 alias tad='tmux attach -d -t'
37 alias ts='tmux new-session -s'
38 alias tl='tmux list-sessions'
39 alias tksv='tmux kill-server'
40 alias tkss='tmux kill-session -t'
41 alias tmuxconf='$EDITOR $ZSH_TMUX_CONFIG'
42
43 # Determine if the terminal supports 256 colors
44 if [[ $terminfo[colors] == 256 ]]; then
45   export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
46 else
47   export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
48 fi
49
50 # Handle $0 according to the standard:
51 # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
52 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
53 0="${${(M)0:#/*}:-$PWD/$0}"
54
55 # Set the correct local config file to use.
56 if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
57   export ZSH_TMUX_CONFIG
58   export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
59 else
60   export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
61 fi
62
63 # Wrapper function for tmux.
64 function _zsh_tmux_plugin_run() {
65   if [[ -n "$@" ]]; then
66     command tmux "$@"
67     return $?
68   fi
69
70   local -a tmux_cmd
71   tmux_cmd=(command tmux)
72   [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
73   [[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
74
75   # Try to connect to an existing session.
76   [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
77
78   # If failed, just run tmux, fixing the TERM variable if requested.
79   if [[ $? -ne 0 ]]; then
80     if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]; then
81       tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
82     elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
83       tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
84     fi
85     if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
86         $tmux_cmd new-session -s $ZSH_TMUX_DEFAULT_SESSION_NAME
87     else
88         $tmux_cmd new-session
89     fi
90   fi
91
92   if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
93     exit
94   fi
95 }
96
97 # Use the completions for tmux for our function
98 compdef _tmux _zsh_tmux_plugin_run
99 # Alias tmux to our wrapper function.
100 alias tmux=_zsh_tmux_plugin_run
101
102 # Autostart if not already in tmux and enabled.
103 if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" ]]; then
104   # Actually don't autostart if we already did and multiple autostarts are disabled.
105   if [[ "$ZSH_TMUX_AUTOSTART_ONCE" == "false" || "$ZSH_TMUX_AUTOSTARTED" != "true" ]]; then
106     export ZSH_TMUX_AUTOSTARTED=true
107     _zsh_tmux_plugin_run
108   fi
109 fi