tmux.plugin.zsh 1.86 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
27
28
29
if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]
then
	if [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
	then
30
		#use this when they have a ~/.tmux.conf
31
		fixed_config="$zsh_tmux_plugin_path/tmux.extra.conf"
32
	else
33
		#use this when they don't have a ~/.tmux.conf
34
		fixed_config="$zsh_tmux_plugin_path/tmux.only.conf"
35
36
37
	fi
fi

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

57
# Alias tmux to our wrapper function.
58
alias tmux=zsh_tmux_plugin_start
59
60
61

if [[ "$ZSH_TMUX_AUTOSTART" == "true" ]]
then
62
	zsh_tmux_plugin_run
63
fi