2014-07-30 14:33:51 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
2014-10-03 16:40:53 +00:00
|
|
|
ALMOST_SENSIBLE_OPTION="@almost-sensible"
|
|
|
|
|
2014-07-30 14:33:51 +00:00
|
|
|
is_osx() {
|
|
|
|
local platform=$(uname)
|
|
|
|
[ "$platform" == "Darwin" ]
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:01:00 +00:00
|
|
|
command_exists() {
|
|
|
|
local command="$1"
|
|
|
|
type "$command" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2014-08-04 15:14:30 +00:00
|
|
|
# returns prefix key, e.g. 'C-a'
|
|
|
|
prefix() {
|
|
|
|
tmux show-option -gv prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
# if prefix is 'C-a', this function returns 'a'
|
|
|
|
prefix_without_ctrl() {
|
|
|
|
local prefix="$(prefix)"
|
|
|
|
echo "$prefix" | cut -d '-' -f2
|
|
|
|
}
|
|
|
|
|
2014-07-30 14:33:51 +00:00
|
|
|
option_value_not_changed() {
|
|
|
|
local option="$1"
|
|
|
|
local default_value="$2"
|
|
|
|
local option_value=$(tmux show-option -gv "$option")
|
2014-08-04 15:13:50 +00:00
|
|
|
[ "$option_value" == "$default_value" ]
|
2014-07-30 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
server_option_value_not_changed() {
|
|
|
|
local option="$1"
|
|
|
|
local default_value="$2"
|
|
|
|
local option_value=$(tmux show-option -sv "$option")
|
2014-08-04 15:13:50 +00:00
|
|
|
[ "$option_value" == "$default_value" ]
|
2014-07-30 14:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key_binding_not_set() {
|
|
|
|
local key="$1"
|
|
|
|
if $(tmux list-keys | grep -q "bind-key[[:space:]]\+${key}"); then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-08-30 20:44:37 +00:00
|
|
|
key_binding_not_changed() {
|
|
|
|
local key="$1"
|
|
|
|
local default_value="$2"
|
|
|
|
if $(tmux list-keys | grep -q "bind-key[[:space:]]\+${key}[[:space:]]\+${default_value}"); then
|
|
|
|
# key still has the default binding
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-10-03 16:40:53 +00:00
|
|
|
almost_sensible_on() {
|
|
|
|
[ "$(tmux show-option -gvq "$ALMOST_SENSIBLE_OPTION")" == "on" ]
|
|
|
|
}
|
|
|
|
|
2014-07-30 14:33:51 +00:00
|
|
|
main() {
|
|
|
|
# OPTIONS
|
|
|
|
|
|
|
|
# enable utf8
|
|
|
|
tmux set-option -g utf8 on
|
|
|
|
|
|
|
|
# enable utf8 in tmux status-left and status-right
|
|
|
|
tmux set-option -g status-utf8 on
|
|
|
|
|
|
|
|
# address vim mode switching delay (http://superuser.com/a/252717/65504)
|
|
|
|
if server_option_value_not_changed "escape-time" "500"; then
|
|
|
|
tmux set-option -s escape-time 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# increase scrollback buffer size
|
|
|
|
if option_value_not_changed "history-limit" "2000"; then
|
|
|
|
tmux set-option -g history-limit 50000
|
|
|
|
fi
|
|
|
|
|
|
|
|
# tmux messages are displayed for 4 seconds
|
|
|
|
if option_value_not_changed "display-time" "750"; then
|
|
|
|
tmux set-option -g display-time 4000
|
|
|
|
fi
|
|
|
|
|
|
|
|
# refresh 'status-left' and 'status-right' more often
|
|
|
|
if option_value_not_changed "status-interval" "15"; then
|
|
|
|
tmux set-option -g status-interval 5
|
|
|
|
fi
|
|
|
|
|
|
|
|
# required (only) on OS X
|
2014-09-21 14:01:00 +00:00
|
|
|
if is_osx && command_exists "reattach-to-user-namespace" && option_value_not_changed "default-command" ""; then
|
2014-08-04 13:47:57 +00:00
|
|
|
tmux set-option -g default-command "reattach-to-user-namespace -l $SHELL"
|
2014-07-30 14:33:51 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# upgrade $TERM
|
|
|
|
if option_value_not_changed "default-terminal" "screen"; then
|
|
|
|
tmux set-option -g default-terminal "screen-256color"
|
|
|
|
fi
|
|
|
|
|
2014-07-30 14:51:42 +00:00
|
|
|
# emacs key bindings in tmux command prompt (prefix + :) are better than
|
|
|
|
# vi keys, even for vim users
|
|
|
|
tmux set-option -g status-keys emacs
|
|
|
|
|
2014-12-12 21:40:09 +00:00
|
|
|
# focus events enabled for terminals that support them
|
|
|
|
tmux set-option -g focus-events on
|
|
|
|
|
2014-10-03 16:40:53 +00:00
|
|
|
# ALMOST SENSIBLE OPTIONS
|
|
|
|
|
|
|
|
if almost_sensible_on; then
|
|
|
|
# C-a should be the Tmux default prefix, really
|
|
|
|
tmux set-option -g prefix C-a
|
|
|
|
tmux set-option -g mode-keys vi
|
|
|
|
|
|
|
|
# enable mouse features for terminals that support it
|
|
|
|
tmux set-option -g mouse-resize-pane on
|
|
|
|
tmux set-option -g mouse-select-pane on
|
|
|
|
tmux set-option -g mouse-select-window on
|
|
|
|
fi
|
|
|
|
|
2014-07-30 14:33:51 +00:00
|
|
|
# DEFAULT KEY BINDINGS
|
|
|
|
|
2014-08-04 15:14:30 +00:00
|
|
|
local prefix="$(prefix)"
|
|
|
|
local prefix_without_ctrl="$(prefix_without_ctrl)"
|
|
|
|
|
|
|
|
# if C-b is not prefix
|
|
|
|
if [ $prefix != "C-b" ]; then
|
|
|
|
# unbind obsolte default binding
|
2014-08-30 20:44:37 +00:00
|
|
|
if key_binding_not_changed "C-b" "send-prefix"; then
|
|
|
|
tmux unbind-key C-b
|
|
|
|
fi
|
2014-08-04 15:14:30 +00:00
|
|
|
|
|
|
|
# pressing `prefix + prefix` sends <prefix> to the shell
|
2014-12-03 01:55:10 +00:00
|
|
|
if key_binding_not_set "$prefix"; then
|
|
|
|
tmux bind-key "$prefix" send-prefix
|
|
|
|
fi
|
2014-07-30 14:33:51 +00:00
|
|
|
fi
|
|
|
|
|
2014-08-04 15:14:30 +00:00
|
|
|
# If Ctrl-a is prefix then `Ctrl-a + a` switches between alternate windows.
|
|
|
|
# Works for any prefix character.
|
|
|
|
if key_binding_not_set "$prefix_without_ctrl"; then
|
|
|
|
tmux bind-key "$prefix_without_ctrl" last-window
|
2014-07-30 14:33:51 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# easier switching between next/prev window
|
|
|
|
if key_binding_not_set "C-p"; then
|
|
|
|
tmux bind-key C-p previous-window
|
|
|
|
fi
|
|
|
|
if key_binding_not_set "C-n"; then
|
|
|
|
tmux bind-key C-n next-window
|
|
|
|
fi
|
|
|
|
|
|
|
|
# source `.tmux.conf` file - as suggested in `man tmux`
|
|
|
|
if key_binding_not_set "R"; then
|
|
|
|
tmux bind-key R run-shell -b ' \
|
|
|
|
tmux source-file ~/.tmux.conf > /dev/null; \
|
|
|
|
tmux display-message "Sourced .tmux.conf!"'
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
main
|