# ==========================
# ===  General settings  ===
# ==========================
set -g default-terminal "screen-256color"
set -g history-limit 50000
#set -sg escape-time 0       # fix for delay in switching modes in vim
# set -g display-time 4000    # Tmux messages are displayed for 4 seconds
# set -g remain-on-exit off
set -g repeat-time 200	 # Allow multiple tmux commands to be run within time period (ms) without pressing the prefix (Default: 500)
setw -g aggressive-resize on

setw -g mode-keys vi    # Prefer vi style key table



# ===========================
# ===  Windows and Panes  ===
# ===========================

#set-hook -g after-new-window 'command-prompt -I "#{window_name}" "rename-window '%%'"' # Prompt to rename window right after it's created

# Set parent terminal title to reflect current window in tmux session
# set -g set-titles on
# set -g set-titles-string "#I:#W"

#setw -g allow-rename off       # Don't allow rename of window name (\o33k...\033\\)
#setw -g automatic-rename off   # Don't rename window based on command being run

set -g base-index 1         # start session number from 1 rather than 0
set -g pane-base-index 1    # start pane number from 1

set -g renumber-windows on	# renumber windows sequentially after closing any of them

# Silence
set-option -g visual-activity off
set-option -g visual-bell off
set-option -g visual-silence off
set-window-option -g monitor-activity off
set-option -g bell-action none



# ======================
# ===  Key bindings  ===
# ======================

# Add secondary prefix of Ctrl-a; easier to type, same to "screen"
# Leave Ctrl-b for legacy purposes
set -g prefix2 C-a
bind C-a send-prefix    # dont mess with other ctrl-a options by pressing ctrl-a twice

bind r source-file ~/.tmux.conf \; display "Config reloaded"    # Reload tmux configuration

# bind c new-window -c "#{pane_current_path}"         # new window and retain cwd
bind | split-window -h -c "#{pane_current_path}"    # split panes vertically
bind - split-window -v -c "#{pane_current_path}"    # split panes horizontally
bind -r Tab last-window # cycle through most recently used tabs
bind T swap-window -t 1 # Make the current window the first window

bind + resize-pane -Z   # zoom on pane
bind -n M-Up copy-mode  # trigger copy mode with PgUp
bind d detach           # detach session

# Force home/end to only move to start/end of line
unbind-key -T copy-mode-vi 'Home'
unbind-key -T copy-mode-vi 'End'
bind-key -T copy-mode-vi 'Home' send -X start-of-line
bind-key -T copy-mode-vi 'End' send -X end-of-line

bind m set -g mouse \; display 'Mouse Mode Toggled'

# Save Last 32000 lines to file
bind-key P command-prompt -p 'save history to filename:' -I '~/tmux.history' 'capture-pane -S -32000 ; save-buffer %1 ; delete-buffer'

# Start capture of current pane
bind-key H pipe-pane -o "exec cat >>$HOME/'tmux-#W.log'" \; display-message 'Toggled logging to $HOME/tmux-#W.log'



# ===================================
# ===  Appearence and status bar  ===
# ===================================
# Load the tmux theme, based on the environment variable TMUX_THEME
# If TMUX_THEME is not set or empty, use ~/.tmux/themes/material.tmux
# Taken from https://github.com/PeterMosmans/dotfiles
run-shell "tmux source-file ~/.tmux/themes/\${TMUX_THEME:-widecentered}.tmux"

set -g status-position top



# ===============
# ===  Final  ===
# ===============
# Load local config, if it exists
if-shell "[ -f ~/.tmux.conf.local ]" 'source ~/.tmux.conf.local'