tmux.plugin.zsh 1.84 KB
Newer Older
1
2
# Configuration variables
# Automatically start tmux
3
[[ -n "$ZSH_TMUX_AUTOSTART" ]] || ZSH_TMUX_AUTOSTART=false
4
# Automatically connect to a previous session if it exists
5
[[ -n "$ZSH_TMUX_AUTOCONNECT" ]] || ZSH_TMUX_AUTOCONNECT=true
6
# Automatically close the terminal when tmux exits
7
[[ -n "$ZSH_TMUX_AUTOQUIT" ]] || ZSH_TMUX_AUTOQUIT=$ZSH_TMUX_AUTOSTART
8
# Set term to screen or screen-256color based on current terminal support
9
[[ -n "$ZSH_TMUX_FIXTERM" ]] || ZSH_TMUX_FIXTERM=true
10
11
12

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

14
15
16
# Determine if the terminal supports 256 colors
if [[ `tput colors` == "256" ]]
then
17
	export ZSH_TMUX_TERM="screen-256color"
18
else
19
	export ZSH_TMUX_TERM="screen"
20
21
fi

22
23
24
# Local variable to store the local config file to use, if any.
local fixed_config=""

25
# Set the correct local config file to use.
26
if [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
27
then
28
29
30
31
32
	#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"
33
34
fi

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

54
# Alias tmux to our wrapper function.
55
alias tmux=zsh_tmux_plugin_start
56

57
if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
58
then
59
	zsh_tmux_plugin_run
60
fi