Tmux

From The World according to Vissie
Jump to navigation Jump to search

My config

Key bindings may be changed with the bind and unbind commands in tmux.conf. For example, the default prefix binding of Ctrl-b can be changed to Ctrl-a by adding the following commands in your configuration file:

unbind C-b
set -g prefix C-a
bind C-a send-prefix

Sane Split Commands Another thing I personally find quite difficult to remember is the pane splitting commands. I mean, seriously? " to split vertically and % to split horizontally? Who’s supposed to memorize that? I find it helpful to have the characters as a visual representation of the split, so I chose | and - for splitting panes:

# split panes using | and -
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %
# switch panes using Alt-arrow without prefix
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

Once you are on tmux 2.1 (or later) you can activate the new mouse mode with a single command:

# Enable mouse mode (tmux 2.1 and above)
set -g mouse on
# use UTF8
set -g utf8
set-window-option -g utf8 on
# make tmux display things in 256 colors
set -g default-terminal "screen-256color"
# set scrollback history to 10000 (10k)
set -g history-limit 10000
# ----------------------
# set some pretty colors
# ----------------------
# set pane colors - hilight the active pane
set-option -g pane-border-fg colour235 #base02
set-option -g pane-active-border-fg colour240 #base01
# colorize messages in the command line
set-option -g message-bg black #base02
set-option -g message-fg brightred #orange
# Kill
bind q kill-window
bind Q kill-session


# force a reload of the config file
unbind r
bind r source-file ~/.tmux.conf
# start window numbering at 1 for easier switching
set -g base-index 1

Maybe look at some Plugins

https://github.com/tmux-plugins/tmux-cpu
https://github.com/tmux-plugins/tmux-battery

Keyboard short-cuts

Ctrl+a "           — split pane horizontally.
Ctrl+a %           — split pane vertically.
Ctrl+a arrow key   — switch pane.
Ctrl+b PgUp/PgDown — Enable scrolling and copy mode. 

Session Management

tmux new -s session_name    — creates a new tmux session named session_name
tmux attach -t session_name — attaches to an existing tmux session named session_name
tmux switch -t session_name — switches to an existing session named session_name
tmux list-sessions          — lists existing tmux sessions
tmux detach (prefix + d)    — detach the currently attached session


One can write a bash script that when run, will create the default session and attach to it. Then you can execute it from a terminal to get the pre-designed configuration in that terminal

#!/bin/bash
tmux new-session -d -n WindowName Command
tmux new-window -n NewWindowName
tmux split-window -v
tmux selectp -t 1
tmux split-window -h
tmux selectw -t 1
tmux -2 attach-session -d

Status bar examples

If you want to know how to format time, have a look at:

https://linux.die.net/man/3/strftime 


Example 1

##
## Appearance
##
# Status bar
set -g status-position bottom
set -g status-interval 4
set -g status-left 
set -g status-right 
set -g status-justify centre # center align window list
setw -g status-bg default
setw -g window-status-current-bg default
setw -g window-status-current-fg default
setw -g window-status-bg default
setw -g window-status-fg white
setw -g window-status-format '#[bg=black]#[fg=black,bold] #I #[bg=default] #[fg=black]#W  '
setw -g window-status-current-format '#[fg=white]#[bg=cyan] #I #[fg=cyan]#[bg=default] #W  '
# Panel borders
set -g pane-active-border-fg cyan
set -g pane-active-border-bg default
set -g pane-border-fg black
set -g pane-border-bg default
# Message text
set-option -g message-bg default
set-option -g message-fg default

Example 2

# Status
set -g status-left 
set -g status-right '#[fg=colour176,bold,bg=colour236,bold] %B #[fg=colour146,bold,bg=colour236,bold]%d, #[fg=colour173,bold,bg=colour236,bold]%Y#[fg=default] #[fg=colour234,bold,bg=colour12,bold] %R '
set -g status-right-length 100
set -g status-bg default
setw -g window-status-format '#[fg=colour236,bold,bg=colour180,bold] #I #[fg=colour180,bold,bg=colour236,bold] #W '
setw -g window-status-current-format '#[fg=colour180,bold,bg=colour236,bold] #I #[fg=colour236,bold,bg=colour180,bold] #W '


Example 3

Issue resolution

I could still see the tmux process was running (ps aux | grep tmux) but for some reason tmux commands would not work to list the existing sessions. Apparently it wasn't finding the existing Unix socket of the still-running tmux process. The fix in this scenario is to locate the existing Unix socket and specify that to tmux using the -S flag; here's how:

You can find the PID of your still-running tmux process with this:

ps -p $(pidof tmux)

Now take your PID (in my case, 6876) and run this to list any open Unix sockets:

sudo lsof -Uap 6876

Hopefully you see output like this:

COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME
tmux    6876  abe    3u  unix 0x0000000000000000      0t0 408477 socket
tmux    6876  abe    4u  unix 0x0000000000000000      0t0 408478 socket
tmux    6876  abe    6u  unix 0x0000000000000000      0t0 408479 /tmp/tmux-1000/default

Now you can specify that existing Unix socket to your tmux command (using the -S flag), and you should be able to list-sessions and attach properly:

tmux -S /tmp/tmux-1000/default list-sessions
tmux -S /tmp/tmux-1000/default attach -t 0