tmux.plugin.zsh 3.04 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
38
# Set the configuration path
: ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
39

Andrew Janke's avatar
Andrew Janke committed
40
# Determine if the terminal supports 256 colors
41
if [[ $terminfo[colors] == 256 ]]; then
Andrew Janke's avatar
Andrew Janke committed
42
43
44
45
  export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
else
  export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
fi
46

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

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

61
62
  local -a tmux_cmd
  tmux_cmd=(command tmux)
63
64
65
  [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)

  # Try to connect to an existing session.
66
  [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
67
68
69

  # If failed, just run tmux, fixing the TERM variable if requested.
  if [[ $? -ne 0 ]]; then
70
71
72
    [[ "$ZSH_TMUX_FIXTERM" == "true" ]] && tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG") || \
    [[ -e "$ZSH_TMUX_CONFIG" ]] && tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
    $tmux_cmd new-session  
73
74
75
76
  fi

  if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
    exit
Andrew Janke's avatar
Andrew Janke committed
77
78
  fi
}
79

Andrew Janke's avatar
Andrew Janke committed
80
81
82
83
# 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
84

Andrew Janke's avatar
Andrew Janke committed
85
# Autostart if not already in tmux and enabled.
86
if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" ]]; then
Andrew Janke's avatar
Andrew Janke committed
87
88
89
90
91
  # 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
92
fi