tmux.plugin.zsh 2.88 KB
Newer Older
1
# Configuration variables
2
#
3
# Automatically start tmux
4
[[ -n "$ZSH_TMUX_AUTOSTART" ]] || ZSH_TMUX_AUTOSTART=false
5
6
7
# Only autostart once. If set to false, tmux will attempt to
# autostart every time your zsh configs are reloaded.
[[ -n "$ZSH_TMUX_AUTOSTART_ONCE" ]] || ZSH_TMUX_AUTOSTART_ONCE=true
8
# Automatically connect to a previous session if it exists
9
[[ -n "$ZSH_TMUX_AUTOCONNECT" ]] || ZSH_TMUX_AUTOCONNECT=true
10
# Automatically close the terminal when tmux exits
11
[[ -n "$ZSH_TMUX_AUTOQUIT" ]] || ZSH_TMUX_AUTOQUIT=$ZSH_TMUX_AUTOSTART
12
# Set term to screen or screen-256color based on current terminal support
13
[[ -n "$ZSH_TMUX_FIXTERM" ]] || ZSH_TMUX_FIXTERM=true
14
15
16
17
18
19
20
21
# 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
[[ -n "$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITHOUT_256COLOR="screen"
# 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
[[ -n "$ZSH_TMUX_FIXTERM_WITH_256COLOR" ]] || ZSH_TMUX_FIXTERM_WITH_256COLOR="screen-256color"
22

23

24
25
# Get the absolute path to the current directory
local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
26

27
28
29
# Determine if the terminal supports 256 colors
if [[ `tput colors` == "256" ]]
then
30
	export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
31
else
32
	export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
33
34
fi

35
36
37
# Local variable to store the local config file to use, if any.
local fixed_config=""

38
# Set the correct local config file to use.
39
if [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
40
then
41
42
43
44
45
	#use this when they have a ~/.tmux.conf
	fixed_config="$zsh_tmux_plugin_path/tmux.extra.conf"
else
	#use this when they don't have a ~/.tmux.conf
	fixed_config="$zsh_tmux_plugin_path/tmux.only.conf"
46
47
fi

48
49
# Wrapper function for tmux.
function zsh_tmux_plugin_run()
50
{
51
	# We have other arguments, just run them
52
	if [[ -n "$@" ]]
53
54
55
56
	then
		\tmux $@
	# Try to connect to an existing session.
	elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]
57
	then
58
		\tmux attach || \tmux `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$fixed_config`  new-session
59
		[[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
60
	# Just run tmux, fixing the TERM variable if requested.
61
	else
62
		\tmux `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$fixed_config`
63
64
65
66
		[[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
	fi
}

67
68
69
# Use the completions for tmux for our function
compdef _tmux zsh_tmux_plugin_run

70
# Alias tmux to our wrapper function.
Josh Matthews's avatar
Josh Matthews committed
71
alias tmux=zsh_tmux_plugin_run
72

73
# Autostart if not already in tmux and enabled.
74
if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
75
then
76
77
78
79
80
81
	# 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
82
fi