summaryrefslogtreecommitdiff
path: root/plugins/tmux
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/tmux')
-rw-r--r--plugins/tmux/README.md42
-rw-r--r--plugins/tmux/tmux.plugin.zsh103
2 files changed, 111 insertions, 34 deletions
diff --git a/plugins/tmux/README.md b/plugins/tmux/README.md
index b4516ef26..aaa81aa17 100644
--- a/plugins/tmux/README.md
+++ b/plugins/tmux/README.md
@@ -1,7 +1,7 @@
# 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.
+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)
@@ -19,26 +19,30 @@ The plugin also supports the following:
| ---------- | -------------------------- | -------------------------------------------------------- |
| `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 |
+| `tds` | `_tmux_directory_session` | Creates or attaches to a session for the current path |
| `tkss` | tmux kill-session -t | Terminate named running tmux session |
+| `tksv` | tmux kill-server | Terminate all running tmux sessions |
+| `tl` | tmux list-sessions | Displays a list of running tmux sessions |
+| `to` | tmux new-session -A -s | Create or attach to a named tmux session |
| `tmux` | `_zsh_tmux_plugin_run` | Start a new tmux session |
| `tmuxconf` | `$EDITOR $ZSH_TMUX_CONFIG` | Open .tmux.conf file with an editor |
-| `tds` | `_tmux_directory_session` | Creates or attaches to a session for the current path |
+| `ts` | tmux new-session -s | Create a new named 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`, `$XDG_CONFIG_HOME/tmux/tmux.conf`) |
-| `ZSH_TMUX_UNICODE` | Set `tmux -u` option to support unicode |
-| `ZSH_TMUX_DEFAULT_SESSION_NAME` | Set tmux default session name when autostart is enabled |
+| Variable | Description |
+| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
+| `ZSH_TMUX_AUTOREFRESH` | Automatically refresh global environments (default: `false`) |
+| `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_CONFIG` | Set the configuration path (default: `$HOME/.tmux.conf`, `$XDG_CONFIG_HOME/tmux/tmux.conf`) |
+| `ZSH_TMUX_DEFAULT_SESSION_NAME` | Set tmux default session name when autostart is enabled |
+| `ZSH_TMUX_AUTONAME_SESSION` | Automatically name new sessions based on the basename of `$PWD` (default: `false`) |
+| `ZSH_TMUX_DETACHED` | Set the detached mode (default: `false`) |
+| `ZSH_TMUX_FIXTERM` | Sets `$TERM` to 256-color term or not based on current terminal support |
+| `ZSH_TMUX_FIXTERM_WITHOUT_256COLOR` | `$TERM` to use for non 256-color terminals (default: `tmux` if available, `screen` otherwise) |
+| `ZSH_TMUX_FIXTERM_WITH_256COLOR` | `$TERM` to use for 256-color terminals (default: `tmux-256color` if available, `screen-256color` otherwise) |
+| `ZSH_TMUX_ITERM2` | Sets the `-CC` option for [iTerm2 tmux integration](https://iterm2.com/documentation-tmux-integration.html) (default: `false`) |
+| `ZSH_TMUX_UNICODE` | Set `tmux -u` option to support unicode |
diff --git a/plugins/tmux/tmux.plugin.zsh b/plugins/tmux/tmux.plugin.zsh
index 72cdd4818..b1d97e98f 100644
--- a/plugins/tmux/tmux.plugin.zsh
+++ b/plugins/tmux/tmux.plugin.zsh
@@ -13,18 +13,32 @@ fi
: ${ZSH_TMUX_AUTOCONNECT:=true}
# Automatically close the terminal when tmux exits
: ${ZSH_TMUX_AUTOQUIT:=$ZSH_TMUX_AUTOSTART}
+# Automatically name the new session based on the basename of PWD
+: ${ZSH_TMUX_AUTONAME_SESSION:=false}
+# Automatically pick up tmux environments
+: ${ZSH_TMUX_AUTOREFRESH:=false}
# Set term to screen or screen-256color based on current terminal support
+: ${ZSH_TMUX_DETACHED:=false}
+# Set detached mode
: ${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
+# Tmux states this should be tmux|screen, but you may need to change it on
# systems without the proper terminfo
-: ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
+if [[ -e /usr/share/terminfo/t/tmux ]]; then
+ : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=tmux}
+else
+ : ${ZSH_TMUX_FIXTERM_WITHOUT_256COLOR:=screen}
+fi
# The TERM to use for 256 color terminals.
-# Tmux states this should be screen-256color, but you may need to change it on
+# Tmux states this should be (tmux|screen)-256color, but you may need to change it on
# systems without the proper terminfo
-: ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
+if [[ -e /usr/share/terminfo/t/tmux-256color ]]; then
+ : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=tmux-256color}
+else
+ : ${ZSH_TMUX_FIXTERM_WITH_256COLOR:=screen-256color}
+fi
# Set the configuration path
if [[ -e $HOME/.tmux.conf ]]; then
: ${ZSH_TMUX_CONFIG:=$HOME/.tmux.conf}
@@ -37,14 +51,42 @@ fi
: ${ZSH_TMUX_UNICODE:=false}
# ALIASES
-alias ta='tmux attach -t'
-alias tad='tmux attach -d -t'
-alias ts='tmux new-session -s'
-alias tl='tmux list-sessions'
+function _build_tmux_alias {
+ setopt localoptions no_rc_expand_param
+ eval "function $1 {
+ if [[ -z \$1 ]] || [[ \${1:0:1} == '-' ]]; then
+ tmux $2 \"\$@\"
+ else
+ tmux $2 $3 \"\$@\"
+ fi
+ }"
+
+ local f s
+ f="_omz_tmux_alias_${1}"
+ s=(${(z)2})
+
+ eval "function ${f}() {
+ shift words;
+ words=(tmux ${@:2} \$words);
+ ((CURRENT+=${#s[@]}+1))
+ _tmux
+ }"
+
+ compdef "$f" "$1"
+}
+
alias tksv='tmux kill-server'
-alias tkss='tmux kill-session -t'
+alias tl='tmux list-sessions'
alias tmuxconf='$EDITOR $ZSH_TMUX_CONFIG'
+_build_tmux_alias "ta" "attach" "-t"
+_build_tmux_alias "tad" "attach -d" "-t"
+_build_tmux_alias "to" "new-session -A" "-s"
+_build_tmux_alias "ts" "new-session" "-s"
+_build_tmux_alias "tkss" "kill-session" "-t"
+
+unfunction _build_tmux_alias
+
# Determine if the terminal supports 256 colors
if [[ $terminfo[colors] == 256 ]]; then
export ZSH_TMUX_TERM=$ZSH_TMUX_FIXTERM_WITH_256COLOR
@@ -77,11 +119,26 @@ function _zsh_tmux_plugin_run() {
[[ "$ZSH_TMUX_ITERM2" == "true" ]] && tmux_cmd+=(-CC)
[[ "$ZSH_TMUX_UNICODE" == "true" ]] && tmux_cmd+=(-u)
+ local _detached=""
+ [[ "$ZSH_TMUX_DETACHED" == "true" ]] && _detached="-d"
+
+ local session_name
+ if [[ "$ZSH_TMUX_AUTONAME_SESSION" == "true" ]]; then
+ # Name the session after the basename of the current directory
+ session_name=${PWD##*/}
+ # If the current directory is the home directory, name it 'HOME'
+ [[ "$PWD" == "$HOME" ]] && session_name="HOME"
+ # If the current directory is the root directory, name it 'ROOT'
+ [[ "$PWD" == "/" ]] && session_name="ROOT"
+ else
+ session_name="$ZSH_TMUX_DEFAULT_SESSION_NAME"
+ fi
+
# Try to connect to an existing session.
- if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
- [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach -t $ZSH_TMUX_DEFAULT_SESSION_NAME
+ if [[ -n "$session_name" ]]; then
+ [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached -t "$session_name"
else
- [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach
+ [[ "$ZSH_TMUX_AUTOCONNECT" == "true" ]] && $tmux_cmd attach $_detached
fi
# If failed, just run tmux, fixing the TERM variable if requested.
@@ -91,8 +148,9 @@ function _zsh_tmux_plugin_run() {
elif [[ -e "$ZSH_TMUX_CONFIG" ]]; then
tmux_cmd+=(-f "$ZSH_TMUX_CONFIG")
fi
- if [[ -n "$ZSH_TMUX_DEFAULT_SESSION_NAME" ]]; then
- $tmux_cmd new-session -s $ZSH_TMUX_DEFAULT_SESSION_NAME
+
+ if [[ -n "$session_name" ]]; then
+ $tmux_cmd new-session -s "$session_name"
else
$tmux_cmd new-session
fi
@@ -103,6 +161,15 @@ function _zsh_tmux_plugin_run() {
fi
}
+# Refresh tmux environment variables.
+function _zsh_tmux_plugin_preexec()
+{
+ local -a tmux_cmd
+ tmux_cmd=(command tmux)
+
+ eval $($tmux_cmd show-environment -s)
+}
+
# Use the completions for tmux for our function
compdef _tmux _zsh_tmux_plugin_run
# Alias tmux to our wrapper function.
@@ -122,10 +189,16 @@ function _tmux_directory_session() {
alias tds=_tmux_directory_session
# Autostart if not already in tmux and enabled.
-if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" ]]; then
+if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" && -z "$ZED_TERM" ]]; 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
+
+# Automatically refresh tmux environments if tmux is running.
+if [[ -n "$TMUX" && "$ZSH_TMUX_AUTOREFRESH" == "true" ]] && tmux ls >/dev/null 2>/dev/null; then
+ autoload -U add-zsh-hook
+ add-zsh-hook preexec _zsh_tmux_plugin_preexec
+fi