pull/57/merge
michaellee8 2020-11-16 16:15:45 +08:00 committed by GitHub
commit 0a92882c6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 25 deletions

18
scripts/tmux_cmd_path.sh Executable file
View File

@ -0,0 +1,18 @@
#!/bin/bash
# Written by michaellee8 <ckmichael8@gmail.com> in Nov 2020 for
# https://github.com/tmux-plugins/tpm/issues/189 to prevent problems caused by
# tmux version mismatch between client and server. This script is
# licensed in public domain.
# Try to get the process ID of the running tmux server,
# would be empty if not found
TMUX_SERVER_PID=$(ps -eo pid=,comm= | grep 'tmux: server' | awk '{ print $1; }')
if [[ -n "$TMUX_SERVER_PID" ]]; then
TMUX_CMD_PATH=$(realpath "/proc/$TMUX_SERVER_PID/exe" 2> /dev/null || echo "tmux" | sed -z '$ s/\n$//')
else
TMUX_CMD_PATH='tmux'
fi
export TMUX_CMD_PATH

View File

@ -1,6 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPTS_DIR="$CURRENT_DIR/scripts"
source "$SCRIPTS_DIR/tmux_cmd_path.sh"
# used to match output from `tmux list-keys` # used to match output from `tmux list-keys`
KEY_BINDING_REGEX="bind-key[[:space:]]\+\(-r[[:space:]]\+\)\?\(-T prefix[[:space:]]\+\)\?" KEY_BINDING_REGEX="bind-key[[:space:]]\+\(-r[[:space:]]\+\)\?\(-T prefix[[:space:]]\+\)\?"
@ -21,7 +24,7 @@ command_exists() {
# returns prefix key, e.g. 'C-a' # returns prefix key, e.g. 'C-a'
prefix() { prefix() {
tmux show-option -gv prefix $TMUX_CMD_PATH show-option -gv prefix
} }
# if prefix is 'C-a', this function returns 'a' # if prefix is 'C-a', this function returns 'a'
@ -33,20 +36,20 @@ prefix_without_ctrl() {
option_value_not_changed() { option_value_not_changed() {
local option="$1" local option="$1"
local default_value="$2" local default_value="$2"
local option_value=$(tmux show-option -gv "$option") local option_value=$($TMUX_CMD_PATH show-option -gv "$option")
[ "$option_value" == "$default_value" ] [ "$option_value" == "$default_value" ]
} }
server_option_value_not_changed() { server_option_value_not_changed() {
local option="$1" local option="$1"
local default_value="$2" local default_value="$2"
local option_value=$(tmux show-option -sv "$option") local option_value=$($TMUX_CMD_PATH show-option -sv "$option")
[ "$option_value" == "$default_value" ] [ "$option_value" == "$default_value" ]
} }
key_binding_not_set() { key_binding_not_set() {
local key="$1" local key="$1"
if $(tmux list-keys | grep -q "${KEY_BINDING_REGEX}${key}[[:space:]]"); then if $($TMUX_CMD_PATH list-keys | grep -q "${KEY_BINDING_REGEX}${key}[[:space:]]"); then
return 1 return 1
else else
return 0 return 0
@ -56,7 +59,7 @@ key_binding_not_set() {
key_binding_not_changed() { key_binding_not_changed() {
local key="$1" local key="$1"
local default_value="$2" local default_value="$2"
if $(tmux list-keys | grep -q "${KEY_BINDING_REGEX}${key}[[:space:]]\+${default_value}"); then if $($TMUX_CMD_PATH list-keys | grep -q "${KEY_BINDING_REGEX}${key}[[:space:]]\+${default_value}"); then
# key still has the default binding # key still has the default binding
return 0 return 0
else else
@ -68,55 +71,55 @@ main() {
# OPTIONS # OPTIONS
# enable utf8 (option removed in tmux 2.2) # enable utf8 (option removed in tmux 2.2)
tmux set-option -g utf8 on 2>/dev/null $TMUX_CMD_PATH set-option -g utf8 on 2>/dev/null
# enable utf8 in tmux status-left and status-right (option removed in tmux 2.2) # enable utf8 in tmux status-left and status-right (option removed in tmux 2.2)
tmux set-option -g status-utf8 on 2>/dev/null $TMUX_CMD_PATH set-option -g status-utf8 on 2>/dev/null
# address vim mode switching delay (http://superuser.com/a/252717/65504) # address vim mode switching delay (http://superuser.com/a/252717/65504)
if server_option_value_not_changed "escape-time" "500"; then if server_option_value_not_changed "escape-time" "500"; then
tmux set-option -s escape-time 0 $TMUX_CMD_PATH set-option -s escape-time 0
fi fi
# increase scrollback buffer size # increase scrollback buffer size
if option_value_not_changed "history-limit" "2000"; then if option_value_not_changed "history-limit" "2000"; then
tmux set-option -g history-limit 50000 $TMUX_CMD_PATH set-option -g history-limit 50000
fi fi
# tmux messages are displayed for 4 seconds # tmux messages are displayed for 4 seconds
if option_value_not_changed "display-time" "750"; then if option_value_not_changed "display-time" "750"; then
tmux set-option -g display-time 4000 $TMUX_CMD_PATH set-option -g display-time 4000
fi fi
# refresh 'status-left' and 'status-right' more often # refresh 'status-left' and 'status-right' more often
if option_value_not_changed "status-interval" "15"; then if option_value_not_changed "status-interval" "15"; then
tmux set-option -g status-interval 5 $TMUX_CMD_PATH set-option -g status-interval 5
fi fi
# required (only) on OS X # required (only) on OS X
if is_osx && command_exists "reattach-to-user-namespace" && option_value_not_changed "default-command" ""; then if is_osx && command_exists "reattach-to-user-namespace" && option_value_not_changed "default-command" ""; then
tmux set-option -g default-command "reattach-to-user-namespace -l $SHELL" $TMUX_CMD_PATH set-option -g default-command "reattach-to-user-namespace -l $SHELL"
fi fi
# upgrade $TERM, tmux 1.9 # upgrade $TERM, tmux 1.9
if option_value_not_changed "default-terminal" "screen"; then if option_value_not_changed "default-terminal" "screen"; then
tmux set-option -g default-terminal "screen-256color" $TMUX_CMD_PATH set-option -g default-terminal "screen-256color"
fi fi
# upgrade $TERM, tmux 2.0+ # upgrade $TERM, tmux 2.0+
if server_option_value_not_changed "default-terminal" "screen"; then if server_option_value_not_changed "default-terminal" "screen"; then
tmux set-option -s default-terminal "screen-256color" $TMUX_CMD_PATH set-option -s default-terminal "screen-256color"
fi fi
# emacs key bindings in tmux command prompt (prefix + :) are better than # emacs key bindings in tmux command prompt (prefix + :) are better than
# vi keys, even for vim users # vi keys, even for vim users
tmux set-option -g status-keys emacs $TMUX_CMD_PATH set-option -g status-keys emacs
# focus events enabled for terminals that support them # focus events enabled for terminals that support them
tmux set-option -g focus-events on $TMUX_CMD_PATH set-option -g focus-events on
# super useful when using "grouped sessions" and multi-monitor setup # super useful when using "grouped sessions" and multi-monitor setup
if ! iterm_terminal; then if ! iterm_terminal; then
tmux set-window-option -g aggressive-resize on $TMUX_CMD_PATH set-window-option -g aggressive-resize on
fi fi
# DEFAULT KEY BINDINGS # DEFAULT KEY BINDINGS
@ -128,34 +131,34 @@ main() {
if [ $prefix != "C-b" ]; then if [ $prefix != "C-b" ]; then
# unbind obsolete default binding # unbind obsolete default binding
if key_binding_not_changed "C-b" "send-prefix"; then if key_binding_not_changed "C-b" "send-prefix"; then
tmux unbind-key C-b $TMUX_CMD_PATH unbind-key C-b
fi fi
# pressing `prefix + prefix` sends <prefix> to the shell # pressing `prefix + prefix` sends <prefix> to the shell
if key_binding_not_set "$prefix"; then if key_binding_not_set "$prefix"; then
tmux bind-key "$prefix" send-prefix $TMUX_CMD_PATH bind-key "$prefix" send-prefix
fi fi
fi fi
# If Ctrl-a is prefix then `Ctrl-a + a` switches between alternate windows. # If Ctrl-a is prefix then `Ctrl-a + a` switches between alternate windows.
# Works for any prefix character. # Works for any prefix character.
if key_binding_not_set "$prefix_without_ctrl"; then if key_binding_not_set "$prefix_without_ctrl"; then
tmux bind-key "$prefix_without_ctrl" last-window $TMUX_CMD_PATH bind-key "$prefix_without_ctrl" last-window
fi fi
# easier switching between next/prev window # easier switching between next/prev window
if key_binding_not_set "C-p"; then if key_binding_not_set "C-p"; then
tmux bind-key C-p previous-window $TMUX_CMD_PATH bind-key C-p previous-window
fi fi
if key_binding_not_set "C-n"; then if key_binding_not_set "C-n"; then
tmux bind-key C-n next-window $TMUX_CMD_PATH bind-key C-n next-window
fi fi
# source `.tmux.conf` file - as suggested in `man tmux` # source `.tmux.conf` file - as suggested in `man tmux`
if key_binding_not_set "R"; then if key_binding_not_set "R"; then
tmux bind-key R run-shell ' \ $TMUX_CMD_PATH bind-key R run-shell ' \
tmux source-file ~/.tmux.conf > /dev/null; \ $TMUX_CMD_PATH source-file ~/.tmux.conf > /dev/null; \
tmux display-message "Sourced .tmux.conf!"' $TMUX_CMD_PATH display-message "Sourced .tmux.conf!"'
fi fi
} }
main main