tmux.plugin.zsh 2.84 KB
Newer Older
1
2
3
4
5
6
if ! (( $+commands[tmux] )); then
  print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." >&2
  return 1
fi

# ALIASES
Kevin Traver's avatar
Kevin Traver committed
7
8

alias ta='tmux attach -t'
9
alias tad='tmux attach -d -t'
Kevin Traver's avatar
Kevin Traver committed
10
11
alias ts='tmux new-session -s'
alias tl='tmux list-sessions'
Peter Butkovic's avatar
Peter Butkovic committed
12
13
alias tksv='tmux kill-server'
alias tkss='tmux kill-session -t'
Kevin Traver's avatar
Kevin Traver committed
14

15
# CONFIGURATION VARIABLES
Andrew Janke's avatar
Andrew Janke committed
16
# Automatically start tmux
17
: ${ZSH_TMUX_AUTOSTART:=false}
Andrew Janke's avatar
Andrew Janke committed
18
19
# Only autostart once. If set to false, tmux will attempt to
# autostart every time your zsh configs are reloaded.
20
: ${ZSH_TMUX_AUTOSTART_ONCE:=true}
Andrew Janke's avatar
Andrew Janke committed
21
# Automatically connect to a previous session if it exists
22
: ${ZSH_TMUX_AUTOCONNECT:=true}
Andrew Janke's avatar
Andrew Janke committed
23
# Automatically close the terminal when tmux exits
24
: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
Andrew Janke's avatar
Andrew Janke committed
25
# Set term to screen or screen-256color based on current terminal support
26
: ${ZSH_TMUX_FIXTERM:=true}
Andrew Janke's avatar
Andrew Janke committed
27
# Set '-CC' option for iTerm2 tmux integration
28
: ${ZSH_TMUX_ITERM2:=false}
Andrew Janke's avatar
Andrew Janke committed
29
30
31
# The TERM to use for non-256 color terminals.
# Tmux states this should be screen, but you may need to change it on
# systems without the proper terminfo
32
: ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
Andrew Janke's avatar
Andrew Janke committed
33
34
35
# The TERM to use for 256 color terminals.
# Tmux states this should be screen-256color, but you may need to change it on
# systems without the proper terminfo
36
: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
37

Andrew Janke's avatar
Andrew Janke committed
38
# Determine if the terminal supports 256 colors
39
if [[ $(tput colors) == 256 ]]; then
Andrew Janke's avatar
Andrew Janke committed
40
41
42
43
  export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
else
  export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
fi
44

Andrew Janke's avatar
Andrew Janke committed
45
# Set the correct local config file to use.
46
if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$HOME/.tmux.conf" ]]; then
Andrew Janke's avatar
Andrew Janke committed
47
  export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
Andrew Janke's avatar
Andrew Janke committed
48
else
Andrew Janke's avatar
Andrew Janke committed
49
  export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
Andrew Janke's avatar
Andrew Janke committed
50
fi
51

Andrew Janke's avatar
Andrew Janke committed
52
53
54
# Wrapper function for tmux.
function _zsh_tmux_plugin_run() {
  if [[ -n "$@" ]]; then
55
56
57
58
59
60
61
62
    command tmux "$@"
    return $?
  fi

  local -a tmux_cmd=(command tmux)
  [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)

  # Try to connect to an existing session.
63
  [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
64
65
66
67
68
69
70
71
72

  # If failed, just run tmux, fixing the TERM variable if requested.
  if [[ $? -ne 0 ]]; then
    [[ "$ZSH_TMUX_FIXTERM" == "true" ]] && tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
    $tmux_cmd new-session
  fi

  if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
    exit
Andrew Janke's avatar
Andrew Janke committed
73
74
  fi
}
75

Andrew Janke's avatar
Andrew Janke committed
76
77
78
79
# Use the completions for tmux for our function
compdef _tmux _zsh_tmux_plugin_run
# Alias tmux to our wrapper function.
alias tmux=_zsh_tmux_plugin_run
80

Andrew Janke's avatar
Andrew Janke committed
81
# Autostart if not already in tmux and enabled.
82
if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]; then
Andrew Janke's avatar
Andrew Janke committed
83
84
85
86
87
  # 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
88
fi