# Rebind the prefix to ^A, like Screen
unbind-key C-b
unbind-key C-a
set -g prefix ^a
bind-key a send-prefix
bind-key C-a select-pane -t :.+

set -g default-terminal "tmux-256color"

set -g mouse on

# increase scrollback buffer size
set -g history-limit 50000

# tmux messages are displayed for 4 seconds
set -g display-time 4000

# address vim mode switching delay (http://superuser.com/a/252717/65504)
set -s escape-time 0

# focus events enabled for terminals that support them
set -g focus-events on

# super useful when using "grouped sessions" and multi-monitor setup
setw -g aggressive-resize on

# emacs key bindings in tmux command prompt (prefix + :) are better than
# vi keys, even for vim users
set -g status-keys emacs

set-window-option -g mode-keys vi
#http://blog.sanctum.geek.nz/vi-mode-in-tmux/
bind-key -T copy-mode-vi "v" send-keys -X begin-selection
#bind-key -T copy-mode-vi "y" send-keys -X copy-selection
# http://unix.stackexchange.com/a/131187
bind-key -T copy-mode-vi "y" send-keys -X copy-pipe 'xclip -in -selection clipboard'
#bind-key -n MouseDown2Pane run -b "xsel -o | tmux load-buffer - && tmux paste-buffer -p"

# Double LMB Select & Copy (Word)
bind-key -T copy-mode-vi DoubleClick1Pane \
    select-pane \; \
    send-keys -X select-word \; \
    send-keys -X copy-pipe "xclip -in -sel primary"
bind-key -n DoubleClick1Pane \
    select-pane \; \
    copy-mode -M \; \
    send-keys -X select-word \; \
    send-keys -X copy-pipe "xclip -in -sel primary"

# Triple LMB Select & Copy (Line)
bind-key -T copy-mode-vi TripleClick1Pane \
    select-pane \; \
    send-keys -X select-line \; \
    send-keys -X copy-pipe "xclip -in -sel primary"
bind-key -n TripleClick1Pane \
    select-pane \; \
    copy-mode -M \; \
    send-keys -X select-line \; \
    send-keys -X copy-pipe "xclip -in -sel primary"

set -g word-separators ' @"=()[]'
set -ag word-separators "'"

# https://justin.abrah.ms/dotfiles/tmux.html
set -g status-bg black
set -g status-fg cyan

# prefix-q
set -g display-panes-colour yellow
set -g display-panes-active-colour cyan
# Display pane numbers for longer
set -g display-panes-time 60000 #60s
set -g pane-active-border-style fg=white

# Smart pane switching with awareness of vim splits
# See: https://github.com/christoomey/vim-tmux-navigator
is_vim='echo "#{pane_current_command}" | grep -iqE "(^|\/)g?(view|n?vim?x?)(diff)?$"'
bind -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind -n C-\\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"

# More memorable split commands
bind | split-window -h
bind - split-window -v

# Bring back clear screen under tmux prefix
bind C-l send-keys 'C-l'

# Easier scrollback - DOES NOT WORK
#bind "^[[5~" eval 'copy' 'stuff ^b'  # PgUp   | Enter copy/scrollback mode and page up
#bind "^[[6~" eval 'copy' 'stuff ^f'  # PgDown | Enter copy/scrollback mode and page down

### Tmux Plugin Manager

#set -g @tpm_plugins '         \
#  tmux-plugins/tpm            \
#'

# Initializes TMUX plugin manager.
# Keep this line at the very bottom of tmux.conf.
# Need to do manualy: Prefix-I to install new plugin. Prefix-U to update.
#run-shell '~/.tmux/plugins/tpm/tpm'

# Combined with 'attach' on the command line, this either starts a new
# session or reattaches a pre-existing one.
# new-session
