summaryrefslogtreecommitdiff
path: root/plugins/tmux
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/tmux')
-rw-r--r--plugins/tmux/README.md41
-rw-r--r--plugins/tmux/tmux.extra.conf2
-rw-r--r--plugins/tmux/tmux.plugin.zsh164
3 files changed, 125 insertions, 82 deletions
diff --git a/plugins/tmux/README.md b/plugins/tmux/README.md
new file mode 100644
index 000000000..db57f5be2
--- /dev/null
+++ b/plugins/tmux/README.md
@@ -0,0 +1,41 @@
+# tmux
+
+This plugin provides aliases for [tmux](https://tmux.github.io/), the terminal multiplexer.
+To use it add `tmux` to the plugins array in your zshrc file.
+
+```zsh
+plugins=(... tmux)
+```
+
+The plugin also supports the following -
+- determines if tmux is installed or not, if not, prompts user to install tmux
+- determines if the terminal supports the 256 colors or not, sets the appropriate configuration variable
+- sets the correct local config file to use
+
+## Aliases
+
+| Alias | Command | Description |
+| ------ | -----------------------|---------------------------------------------------------- |
+| `ta` | tmux attach -t | Attach new tmux session to already running named session |
+| `tad` | tmux attach -d -t | Detach named tmux session |
+| `ts` | tmux new-session -s | Create a new named tmux session |
+| `tl` | tmux list-sessions | Displays a list of running tmux sessions |
+| `tksv` | tmux kill-server | Terminate all running tmux sessions |
+| `tkss` | tmux kill-session -t | Terminate named running tmux session |
+| `tmux` | `_zsh_tmux_plugin_run` | Start a new tmux session |
+
+
+## Configuration Variables
+
+| Variable | Description |
+|-------------------------------------|-------------------------------------------------------------------------------|
+| `ZSH_TMUX_AUTOSTART` | Automatically starts tmux (default: `false`) |
+| `ZSH_TMUX_AUTOSTART_ONCE` | Autostart only if tmux hasn't been started previously (default: `true`) |
+| `ZSH_TMUX_AUTOCONNECT` | Automatically connect to a previous session if it exits (default: `true`) |
+| `ZSH_TMUX_AUTOQUIT` | Automatically closes terminal once tmux exits (default: `ZSH_TMUX_AUTOSTART`) |
+| `ZSH_TMUX_FIXTERM` | Sets `$TERM` to 256-color term or not based on current terminal support |
+| `ZSH_TMUX_ITERM2` | Sets the `-CC` option for iTerm2 tmux integration (default: `false`) |
+| `ZSH_TMUX_FIXTERM_WITHOUT_256COLOR` | `$TERM` to use for non 256-color terminals (default: `screen`) |
+| `ZSH_TMUX_FIXTERM_WITH_256COLOR` | `$TERM` to use for 256-color terminals (default: `screen-256color` |
+| `ZSH_TMUX_CONFIG` | Set the configuration path (default: `$HOME/.tmux.conf`) |
+| `ZSH_TMUX_UNICODE` | Set `tmux -u` option to support unicode |
diff --git a/plugins/tmux/tmux.extra.conf b/plugins/tmux/tmux.extra.conf
index beffd380c..c4aaad0b0 100644
--- a/plugins/tmux/tmux.extra.conf
+++ b/plugins/tmux/tmux.extra.conf
@@ -1,2 +1,2 @@
set -g default-terminal $ZSH_TMUX_TERM
-source $HOME/.tmux.conf
+source-file $ZSH_TMUX_CONFIG \ No newline at end of file
diff --git a/plugins/tmux/tmux.plugin.zsh b/plugins/tmux/tmux.plugin.zsh
index bc32c8907..e52443a71 100644
--- a/plugins/tmux/tmux.plugin.zsh
+++ b/plugins/tmux/tmux.plugin.zsh
@@ -1,97 +1,99 @@
-#
-# Aliases
-#
+if ! (( $+commands[tmux] )); then
+ print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin." >&2
+ return 1
+fi
+
+# ALIASES
alias ta='tmux attach -t'
+alias tad='tmux attach -d -t'
alias ts='tmux new-session -s'
alias tl='tmux list-sessions'
alias tksv='tmux kill-server'
alias tkss='tmux kill-session -t'
-# Only run if tmux is actually installed
-if which tmux &> /dev/null
- then
- # Configuration variables
- #
- # Automatically start tmux
- [[ -n "$ZSH_TMUX_AUTOSTART" ]] || ZSH_TMUX_AUTOSTART=false
- # 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
- # Automatically connect to a previous session if it exists
- [[ -n "$ZSH_TMUX_AUTOCONNECT" ]] || ZSH_TMUX_AUTOCONNECT=true
- # Automatically close the terminal when tmux exits
- [[ -n "$ZSH_TMUX_AUTOQUIT" ]] || ZSH_TMUX_AUTOQUIT=$ZSH_TMUX_AUTOSTART
- # Set term to screen or screen-256color based on current terminal support
- [[ -n "$ZSH_TMUX_FIXTERM" ]] || ZSH_TMUX_FIXTERM=true
- # Set '-CC' option for iTerm2 tmux integration
- [[ -n "$ZSH_TMUX_ITERM2" ]] || ZSH_TMUX_ITERM2=false
- # 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"
+# CONFIGURATION VARIABLES
+# Automatically start tmux
+: ${ZSH_TMUX_AUTOSTART:=false}
+# Only autostart once. If set to false, tmux will attempt to
+# autostart every time your zsh configs are reloaded.
+: ${ZSH_TMUX_AUTOSTART_ONCE:=true}
+# Automatically connect to a previous session if it exists
+: ${ZSH_TMUX_AUTOCONNECT:=true}
+# Automatically close the terminal when tmux exits
+: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
+# Set term to screen or screen-256color based on current terminal support
+: ${ZSH_TMUX_FIXTERM:=true}
+# Set '-CC' option for iTerm2 tmux integration
+: ${ZSH_TMUX_ITERM2:=false}
+# 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
+: ${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
+: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
+# Set the configuration path
+: ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
+# Set -u option to support unicode
+: ${ZSH_TMUX_UNICODE:=false}
+
+# Determine if the terminal supports 256 colors
+if [[ $terminfo[colors] == 256 ]]; then
+ export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
+else
+ export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
+fi
+# Set the correct local config file to use.
+if [[ "$ZSH_TMUX_ITERM2" == "false" && -e "$ZSH_TMUX_CONFIG" ]]; then
+ export ZSH_TMUX_CONFIG
+ export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.extra.conf"
+else
+ export _ZSH_TMUX_FIXED_CONFIG="${0:h:a}/tmux.only.conf"
+fi
- # Get the absolute path to the current directory
- local zsh_tmux_plugin_path="$(cd "$(dirname "$0")" && pwd)"
+# Wrapper function for tmux.
+function _zsh_tmux_plugin_run() {
+ if [[ -n "$@" ]]; then
+ command tmux "$@"
+ return $?
+ fi
- # Determine if the terminal supports 256 colors
- if [[ `tput colors` == "256" ]]
- then
- export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
- else
- export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITHOUT_256COLOR
- fi
+ local -a tmux_cmd
+ tmux_cmd=(command tmux)
+ [[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
+ [[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
- # Set the correct local config file to use.
- if [[ "$ZSH_TMUX_ITERM2" == "false" ]] && [[ -f $HOME/.tmux.conf || -h $HOME/.tmux.conf ]]
- then
- #use this when they have a ~/.tmux.conf
- export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.extra.conf"
- else
- #use this when they don't have a ~/.tmux.conf
- export _ZSH_TMUX_FIXED_CONFIG="$zsh_tmux_plugin_path/tmux.only.conf"
- fi
+ # Try to connect to an existing session.
+ [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
- # Wrapper function for tmux.
- function _zsh_tmux_plugin_run()
- {
- # We have other arguments, just run them
- if [[ -n "$@" ]]
- then
- \tmux $@
- # Try to connect to an existing session.
- elif [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]]
- then
- \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` attach || \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG` new-session
- [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
- # Just run tmux, fixing the TERM variable if requested.
- else
- \tmux `[[ "$ZSH_TMUX_ITERM2" == "true" ]] && echo '-CC '` `[[ "$ZSH_TMUX_FIXTERM" == "true" ]] && echo '-f '$_ZSH_TMUX_FIXED_CONFIG`
- [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]] && exit
- fi
- }
+ # If failed, just run tmux, fixing the TERM variable if requested.
+ if [[ $? -ne 0 ]]; then
+ if [[ "$ZSH_TMUX_FIXTERM" == "true" ]]; then
+ tmux_cmd+=(-f "$_ZSH_TMUX_FIXED_CONFIG")
+ elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
+ tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
+ fi
+ $tmux_cmd new-session
+ fi
- # Use the completions for tmux for our function
- compdef _tmux _zsh_tmux_plugin_run
+ if [[ "$ZSH_TMUX_AUTOQUIT" == "true" ]]; then
+ exit
+ fi
+}
- # Alias tmux to our wrapper function.
- alias tmux=_zsh_tmux_plugin_run
+# 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
- # Autostart if not already in tmux and enabled.
- if [[ ! -n "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" ]]
- then
- # 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
- fi
-else
- print "zsh tmux plugin: tmux not found. Please install tmux before using this plugin."
+# Autostart if not already in tmux and enabled.
+if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" ]]; then
+ # 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
fi