#---------------------------------------------------------------------------#
# .tmux.conf
# Helmut K. C. Tessarek, Last update 2018-10-16
#---------------------------------------------------------------------------#

#---------------------------------------------------------------------------#
# set prefix key to ctrl+a / ctrl-b is used in vi for going back one page
#---------------------------------------------------------------------------#
unbind C-b
set -g prefix C-a

#---------------------------------------------------------------------------#
# send the prefix to client inside window (nested sessions)
#---------------------------------------------------------------------------#
bind-key a send-prefix

#---------------------------------------------------------------------------#
# toggle last window like screen
#---------------------------------------------------------------------------#
bind-key C-a last-window

#---------------------------------------------------------------------------#
# start window indexing at one instead of zero
#---------------------------------------------------------------------------#
#set -g base-index 1

#---------------------------------------------------------------------------#
# default terminal - we want 256 colors !!!
#---------------------------------------------------------------------------#
set -g default-terminal "screen-256color"

#---------------------------------------------------------------------------#
# on-screen time for status messages in ms
#---------------------------------------------------------------------------#
set -g display-time 2000

#---------------------------------------------------------------------------#
# on-screen time for display-panes in ms
#---------------------------------------------------------------------------#
set -g display-panes-time 2000

#---------------------------------------------------------------------------#
# color for display pane indicator
#---------------------------------------------------------------------------#
set -g display-panes-colour "cyan"
#set -g display-panes-active-colour "#0087ff"
#set -g display-panes-active-colour "red"

#---------------------------------------------------------------------------#
# open a man page in new window
#---------------------------------------------------------------------------#
unbind m
bind m command-prompt "split-window 'exec man %%'"

#---------------------------------------------------------------------------#
# quick view of processes
#---------------------------------------------------------------------------#
#bind '~' split-window "exec htop"

#---------------------------------------------------------------------------#
# scrollback buffer n lines
#---------------------------------------------------------------------------#
set -g history-limit 5000

#---------------------------------------------------------------------------#
# toggle status bar
#---------------------------------------------------------------------------#
unbind b
bind-key b set-option status

#---------------------------------------------------------------------------#
# resize panes like vim
# feel free to change the "1" to however many lines you want to resize by,
# only one at a time can be slow
#---------------------------------------------------------------------------#
unbind <
unbind >
unbind -
unbind +
bind -r < resize-pane -L 1
bind -r > resize-pane -R 1
bind -r - resize-pane -D 1
bind -r + resize-pane -U 1

#---------------------------------------------------------------------------#
# toggle mouse helpers
#---------------------------------------------------------------------------#
unbind Enter
unbind C-m
bind C-m set-option mouse \; display-message 'mouse -> #{?mouse,on,off}'

#---------------------------------------------------------------------------#
# Reload config file
#---------------------------------------------------------------------------#
unbind R
bind-key R source-file ~/.tmux.conf \; display-message "Reloading configuration done"

#---------------------------------------------------------------------------#
# start ssh session in new window
#---------------------------------------------------------------------------#
unbind S
bind-key S command-prompt "new-window -n %1 'ssh %1'"

#---------------------------------------------------------------------------#
# start new session
#---------------------------------------------------------------------------#
unbind C
bind-key C command-prompt "new-session -s %1"

#---------------------------------------------------------------------------#
# Keys to switch session
#---------------------------------------------------------------------------#
bind Q switchc -t0
bind W switchc -t compile
bind E switchc -t config

#---------------------------------------------------------------------------#
# break pane in background
#---------------------------------------------------------------------------#
unbind '@'
bind '@' break-pane -d

#---------------------------------------------------------------------------#
# join pane with target window
#---------------------------------------------------------------------------#
unbind ^
bind ^ command-prompt "join-pane -t %1"

#---------------------------------------------------------------------------#
# move around panes with hjkl, as one would in vim after pressing ctrl-w
#---------------------------------------------------------------------------#
#bind h select-pane -L
#bind j select-pane -D
#bind k select-pane -U
#bind l select-pane -R

#---------------------------------------------------------------------------#
# bind : to command-prompt like vim
# this is the default in tmux already
#---------------------------------------------------------------------------#
bind : command-prompt

#---------------------------------------------------------------------------#
# Remain on exit
#---------------------------------------------------------------------------#
#setw -g remain-on-exit on

#---------------------------------------------------------------------------#
# vi-style controls for copy mode
#---------------------------------------------------------------------------#
setw -g mode-keys vi

#---------------------------------------------------------------------------#
# Make mouse useful in copy mode
#---------------------------------------------------------------------------#
#setw -g mode-mouse on

#---------------------------------------------------------------------------#
# More straight forward key bindings for splitting
#---------------------------------------------------------------------------#
unbind %
unbind v
#bind | split-window -h
bind v split-window -h
unbind '"'
unbind h
#bind - split-window -v
bind h split-window -v

#---------------------------------------------------------------------------#
# Synchronize panes
#---------------------------------------------------------------------------#
unbind y
bind y set-window-option synchronize-panes \; display-message 'synchronize-panes -> #{?synchronize-panes,on,off}'

#---------------------------------------------------------------------------#
# Other key codes: Tab, BTab, Escape
#---------------------------------------------------------------------------#

#---------------------------------------------------------------------------#
# Clock
#---------------------------------------------------------------------------#
setw -g clock-mode-colour green
setw -g clock-mode-style 24

#---------------------------------------------------------------------------#
# Terminal emulator window title
#---------------------------------------------------------------------------#
set -g set-titles on
set -g set-titles-string '#S:#I.#P #W'

#---------------------------------------------------------------------------#
# Status Bar
#---------------------------------------------------------------------------#
set -g status-bg black
set -g status-fg white
set -g status-interval 1
set -g status-left-length 30
set -g status-left '#[fg=green]#h#[default] '
#set -g status-right '#[fg=yellow]#(cut -d " " -f 1-4 /proc/loadavg)#[default] #[fg=cyan,bold]%Y-%m-%d %H:%M:%S#[default]'
#set -g status-right '#[fg=yellow,bold]%Y-%m-%d %H:%M#[default]'
set -g status-right '#[fg=yellow]%Y-%m-%d %H:%M %Z#[default]'
#set -g status-justify center
#set -g status-keys vi

set -g allow-rename off
setw -g automatic-rename on

#---------------------------------------------------------------------------#
# Highlighting the active window in status bar
#---------------------------------------------------------------------------#
#setw -g window-status-current-bg red
set-option -g window-status-format "#I:#W#F#{?pane_synchronized,S,}"
set-window-option -g window-status-current-format "#{?pane_synchronized,#[bg=red],}#{?window_zoomed_flag,#[bg=colour130],}#I:#W#F#{?pane_synchronized,S,}"
set-option -g window-status-current-style bg=blue

#---------------------------------------------------------------------------#
# global update environment
#---------------------------------------------------------------------------#
set -g update-environment "DISPLAY SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION WINDOWID XAUTHORITY TZ"

#---------------------------------------------------------------------------#
# settings for AIX
# terminal overrides to enable colors
# set default terminal to vt100 or xterm (screen does not exist on AIX)
#---------------------------------------------------------------------------#
if-shell "uname|grep AIX" 'set -g terminal-overrides "xterm*:XT,xterm*:setab=\\E[4%p1%dm,xterm*:setaf=\\E[3%p1%dm"'
#if-shell "uname|grep AIX" "set -g default-terminal vt100"
if-shell "uname|grep AIX" "set -g default-terminal xterm"

#---------------------------------------------------------------------------#
# settings for macOS
#---------------------------------------------------------------------------#
if-shell "uname|grep Darwin" 'set -g default-command "/bin/bash -l"'

#---------------------------------------------------------------------------#
# Pane coloring
# set inactive/active window styles
#---------------------------------------------------------------------------#
set -g window-style "fg=colour247,bg=colour234"
set -g window-active-style "fg=colour250,bg=black"
set -g @TPCS "1"

#---------------------------------------------------------------------------#
# pane border - different style / use cyan
#---------------------------------------------------------------------------#
#set -g pane-border-bg colour235
#set -g pane-border-fg colour238
#set -g pane-active-border-bg colour234
#set -g pane-active-border-fg colour51

#---------------------------------------------------------------------------#
# toggle pane coloring on/off
#---------------------------------------------------------------------------#
unbind C-b
bind C-b if -F '#{@TPCS}' \
	'set -g window-style "fg=default,bg=default" ; set -g window-active-style "fg=default,bg=default" ; set -g @TPCS "0"; display-message "Pane coloring -> off"' \
	'set -g window-style "fg=colour247,bg=colour234" ; set -g window-active-style "fg=colour250,bg=black" ; set -g @TPCS "1"; display-message "Pane coloring -> on"'

#---------------------------------------------------------------------------#
# List of plugins
#---------------------------------------------------------------------------#
set -g @plugin 'tmux-plugins/tpm'
#set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-logging'

set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-save-bash-history 'on'

set -g @logging-path $HOME
set -g @screen-capture-path $HOME
set -g @save-complete-history-path $HOME

# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'git@github.com/user/plugin'
# set -g @plugin 'git@bitbucket.com/user/plugin'

#---------------------------------------------------------------------------#
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
#---------------------------------------------------------------------------#
run '~/.tmux/plugins/tpm/tpm'
