Linux :: Tmux

Tmux

Tips

# command can be shortened as long as it's not ambuigous
tmux new -s name    # same as new-session

# start tmux automatically when in SSH
# exit at the end will automatically exit when detach
# tmux new -As name     # -A attach if exists
if [[ -n $SSH_CLIENT && $(command -v tmux) ]]; then
    [[ -z $TMUX ]] && { tmux attach -t ssh || exec tmux new -s ssh && exit; }
fi

# multiple commands
tmux start-server \; source foo.session
tmux new-session \; split-window -v -p 50 \; split-window -h -p 50 \; select-pane -t 0 \; split-window -h -p 50 \;

# exit
function exit {
    [[ -z $TMUX ]] && builtin exit || tmux detach
}

Commands

Tmux-server/client -> sessions -> windows -> panes
# to reference, use different prefix
$1  # first sesseion
@2  # second window
%3  # third pane

# Tmux-server, independent of terminal
tmux start-server   # has no effect if there is no session
tmux kill-server    # kill every session within the server

# sessions
tmux ls                     # or list-sessions
tmux [new -s name]
    -s name     # name of the session
    -A          # attach if the session exists
    -d          # detach - put it to background
    -n name     # create a window with name
tmux attach -t name     # attach to target session
tmux switch -t name     # switch to target session
    prefix + s          # list sessions
tmux detach -t name     # detach target session
tmux kill-session -t name   # kill the session with name

tmux attach -t name         # or number
tmux detach

# windows commands
tmux new-window -a -t session: -n name
    -a      # append
    -t      # target, use : to specify order
prefix + :
    new-window
    select-window
    rename-window
    kill-window
    split-window    # vertical
    split-window -h # horizontal

# pane commands
prefix + :
    swap-pane -[UDLR]
    select-pane -[UDLR]
    select-pane -t :.+      # pane cycling

Conf

~/.tmux.conf

# prefix key, -g means global; -w for window
unbind C-b
set -g prefix C-a   # replace b with other, use '' for specicial char
bind-key C-a send-prefix

# base from 1
set -g base-index 1
set -g pane-base-index 1

# terminal
# tmux-256color will cause issues in macOS
set -g default-terminal screen-256color

# short cut to reload the config file
unbind r
bind r source-file ~/.tmux.conf \; display "Reload ~/.tmux.conf"

# mouse mode
set -g mouse on

# stop renaming windows with the last command
set-option -g allow-rename off

# set terminal title
set-option -g set-titles on

# windows
# default mode is insert mode in vi
# set vi mode in copy mode
prefix + [     # switch to copy mode, q to quit
set-window-option -g mode-keys vi

# set copy keystroke to vi, v to select and y to copy
unbind -T copy-mode-vi Space; # default for begin selection
unbind -T copy-mode-vi Enter;
bind -T copy-mode-vi v send-keys -X begin-selection
# use xsel or xclip; default paste from buffer is prefix-key -> ]
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xsel --clipboard"
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -i -f -selection primary | xclip -i -selection clipboard"


# nav in windows
prefix-key -> Num   # select window with number
unbind n    # default to move to next windows
unbind w    # default to change current window interactively
bind n command-prompt "rename-window '%%'"  # rename current window
bind w new-window -c "#{pane_current_path}" # create new window
## move window
## TODO meta key conflicts with Esc
#bind -n M-j previous-window # use Alt+j for previous
#bind -n M-k next-window     # use Alt+k for next window


# split panes
# use v and h
unbind v
unbind h
unbind %    # default vertical split
unbind '"'  # default horizontal split
# -h and -v appeared to flipped... -c exec shell command to go back to pane
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# resize
prefix-key -> alt+arrow

# nav panes
prefix-key -> arrow
## use C+hjkl to nav without pre-key (-n)
bind -n C-h select-pane -L
bind -n C-j select-pane -D
bind -n C-k select-pane -U
bind -n C-l select-pane -R
# -r repeatable
bind -r Tab select-pane -t :.+

# number of history
set -g history-limit 100000

# color

# highlight current pane
## inactive/active window
set -g window-style 'fg=colour247,bg=colour236'
set -g window-active-style 'fg=colour250,bg=black'

## pane border
set -g pane-border-style 'fg=colour238,bg=colour235'
set -g pane-active-border-style 'fg=colour51,bg=colour236'

######## advanced

# env
#h      host
#S      session name
#W      window title
#P      pane number

## hook
set-hook -g client-attached 'source-file ~/.tmux.conf'
## unhook
set-hook -u client-attached
tmux show-hooks

# var
wg_var="#[bg=colour71,fg=black]ssh@#h"
wg_zoom_indicator="#[fg=white,bg=brightcyan]#{?window_zoomed_flag,[Z],}#[default]"
wg_prefix_indicator="#{?client_prefix,#[fg=brightwhite]#[bg=red]Prefix-,}"

# if statement
%if #{==:#{SSH_CONNECTION},}
    wg_host_indicator="#[bg=colour71,fg=black]local@#h"
%else

if-shell -b '[[ $(uname) == Darwin ]]' "\
    wg_mem=\"#(top -l 1 -s 0 | grep PhysMem | cut -d\' \' -f2 -f6)\" \
    " "\
    wg_mem=\"#(free -ht | grep Total | awk -F\' \' \'{ print $3 $2 }\')\" \
    "