tmux.plugin.zsh 2.27 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
# Get the absolute path to the current directory
local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
18

19
20
21
# Determine if the terminal supports 256 colors
if [[ `tput colors` == "256" ]]
then
22
	export ZSH_TMUX_TERM="screen-256color"
23
else
24
	export ZSH_TMUX_TERM="screen"
25
26
fi

27
28
29
# Local variable to store the local config file to use, if any.
local fixed_config=""

30
# Set the correct local config file to use.
31
if [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
32
then
33
34
35
36
37
	#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"
38
39
fi

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

59
# Alias tmux to our wrapper function.
Josh Matthews's avatar
Josh Matthews committed
60
alias tmux=zsh_tmux_plugin_run
61

62
# Autostart if not already in tmux and enabled.
63
if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
64
then
65
66
67
68
69
70
	# 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
71
fi