Skip to content

Instantly share code, notes, and snippets.

@Kvieta1990
Created March 20, 2023 04:53
Show Gist options
  • Save Kvieta1990/20f9df20b80b63aaf71341e390c4e8f4 to your computer and use it in GitHub Desktop.
Save Kvieta1990/20f9df20b80b63aaf71341e390c4e8f4 to your computer and use it in GitHub Desktop.
.tmux.conf
# : << EOF
# https://github.com/gpakosz/.tmux
# (‑●‑●)> dual licensed under the WTFPL v2 license and the MIT license,
# without any warranty.
# Copyright 2012— Gregory Pakosz (@gpakosz).
# /!\ do not edit this file
# instead, override settings in ~/.tmux.conf.local, see README.md
# -- general -------------------------------------------------------------------
set -g default-terminal "screen-256color"
if 'infocmp -x tmux-256color > /dev/null 2>&1' 'set -g default-terminal "tmux-256color"'
setw -g xterm-keys on
set -s escape-time 10 # faster command sequences
set -sg repeat-time 600 # increase repeat timeout
set -s focus-events on
set -g prefix2 C-a # GNU-Screen compatible prefix
bind C-a send-prefix -2
set -q -g status-utf8 on # expect UTF-8 (tmux < 2.2)
setw -q -g utf8 on
set -g history-limit 5000 # boost history
# edit configuration
bind e new-window -n "~/.tmux.conf.local" sh -c '${EDITOR:-vim} ~/.tmux.conf.local && tmux source ~/.tmux.conf && tmux display "~/.tmux.conf sourced"'
# reload configuration
bind r source-file ~/.tmux.conf \; display '~/.tmux.conf sourced'
# -- display -------------------------------------------------------------------
set -g base-index 1 # start windows numbering at 1
setw -g pane-base-index 1 # make pane numbering consistent with windows
setw -g automatic-rename on # rename window to reflect current program
set -g renumber-windows on # renumber windows when a window is closed
set -g set-titles on # set terminal title
set -g display-panes-time 800 # slightly longer pane indicators display time
set -g display-time 1000 # slightly longer status messages display time
set -g status-interval 10 # redraw status line every 10 seconds
# clear both screen and history
bind -n C-l send-keys C-l \; run 'sleep 0.2' \; clear-history
# activity
set -g monitor-activity on
set -g visual-activity off
# -- navigation ----------------------------------------------------------------
# create session
bind C-c new-session
# find session
bind C-f command-prompt -p find-session 'switch-client -t %%'
# session navigation
bind BTab switch-client -l # move to last session
# split current window horizontally
bind - split-window -v
# split current window vertically
bind _ split-window -h
# pane navigation
# bind -r h select-pane -L # move left
# bind -r j select-pane -D # move down
# bind -r k select-pane -U # move up
# bind -r l select-pane -R # move right
bind > swap-pane -D # swap current pane with the next one
bind < swap-pane -U # swap current pane with the previous one
# maximize current pane
bind + run 'cut -c3- ~/.tmux.conf | sh -s _maximize_pane "#{session_name}" #D'
# pane resizing
bind -r H resize-pane -L 2
bind -r J resize-pane -D 2
bind -r K resize-pane -U 2
bind -r L resize-pane -R 2
# window navigation
unbind n
unbind p
# bind -r C-h previous-window # select previous window
# bind -r C-l next-window # select next window
bind -n C-S-Up last-window # move to last active window
bind -n C-S-Left previous-window # select previous window
bind -n C-S-Right next-window # select next window
# toggle mouse
bind m run "cut -c3- ~/.tmux.conf | sh -s _toggle_mouse"
# -- urlview -------------------------------------------------------------------
bind U run "cut -c3- ~/.tmux.conf | sh -s _urlview #{pane_id}"
# -- facebook pathpicker -------------------------------------------------------
bind F run "cut -c3- ~/.tmux.conf | sh -s _fpp #{pane_id} #{pane_current_path}"
# -- list choice (tmux < 2.4) --------------------------------------------------
# vi-choice is gone in tmux >= 2.4
run -b 'tmux bind -t vi-choice h tree-collapse 2> /dev/null || true'
run -b 'tmux bind -t vi-choice l tree-expand 2> /dev/null || true'
run -b 'tmux bind -t vi-choice K start-of-list 2> /dev/null || true'
run -b 'tmux bind -t vi-choice J end-of-list 2> /dev/null || true'
run -b 'tmux bind -t vi-choice H tree-collapse-all 2> /dev/null || true'
run -b 'tmux bind -t vi-choice L tree-expand-all 2> /dev/null || true'
run -b 'tmux bind -t vi-choice Escape cancel 2> /dev/null || true'
# -- edit mode (tmux < 2.4) ----------------------------------------------------
# vi-edit is gone in tmux >= 2.4
run -b 'tmux bind -ct vi-edit H start-of-line 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit L end-of-line 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit q cancel 2> /dev/null || true'
run -b 'tmux bind -ct vi-edit Escape cancel 2> /dev/null || true'
# -- copy mode -----------------------------------------------------------------
bind Enter copy-mode # enter copy mode
run -b 'tmux bind -t vi-copy v begin-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi v send -X begin-selection 2> /dev/null || true'
run -b 'tmux bind -t vi-copy C-v rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi C-v send -X rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -t vi-copy y copy-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi y send -X copy-selection-and-cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy Escape cancel 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi Escape send -X cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy H start-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi H send -X start-of-line 2> /dev/null || true'
run -b 'tmux bind -t vi-copy L end-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi L send -X end-of-line 2> /dev/null || true'
# copy to X11 clipboard
if -b 'command -v xsel > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xsel -i -b"'
if -b '! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xclip -i -selection clipboard >/dev/null 2>&1"'
# copy to Wayland clipboard
if -b 'command -v wl-copy > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | wl-copy"'
# copy to macOS clipboard
if -b 'command -v pbcopy > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | pbcopy"'
if -b 'command -v reattach-to-user-namespace > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | reattach-to-user-namespace pbcopy"'
# copy to Windows clipboard
if -b 'command -v clip.exe > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | clip.exe"'
if -b '[ -c /dev/clipboard ]' 'bind y run -b "tmux save-buffer - > /dev/clipboard"'
# -- buffers -------------------------------------------------------------------
bind b list-buffers # list paste buffers
# bind p paste-buffer -p # paste from the top paste buffer
bind P choose-buffer # choose which buffer to paste from
# -- 8< ------------------------------------------------------------------------
source -q ~/.tmux.conf.local
run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
#!/usr/bin/env bash
# Save and restore the state of tmux sessions and windows.
# TODO: persist and restore the state & position of panes.
set -e
dump() {
local d=$'\t'
tmux list-windows -a -F "#S${d}#W${d}#{pane_current_path}"
}
save() {
dump > ~/.tmux-session
}
terminal_size() {
stty size 2>/dev/null | awk '{ printf "-x%d -y%d", $2, $1 }'
}
session_exists() {
tmux has-session -t "$1" 2>/dev/null
}
add_window() {
tmux new-window -d -t "$1:" -n "$2" -c "$3"
}
new_session() {
cd "$3" &&
tmux new-session -d -s "$1" -n "$2" $4
}
restore() {
tmux start-server
local count=0
local dimensions="$(terminal_size)"
while IFS=$'\t' read session_name window_name dir; do
if [[ -d "$dir" && $window_name != "log" && $window_name != "man" ]]; then
if session_exists "$session_name"; then
add_window "$session_name" "$window_name" "$dir"
else
new_session "$session_name" "$window_name" "$dir" "$dimensions"
count=$(( count + 1 ))
fi
fi
done < ~/.tmux-session
echo "restored $count sessions"
}
case "$1" in
save | restore )
$1
;;
* )
echo "valid commands: save, restore" >&2
exit 1
esac
@Kvieta1990
Copy link
Author

This is my tmux configuration file backup, together with a useful script for saving and loading tmux sessions to and from a file at home directory. The script originates from the following Q&A,

https://superuser.com/questions/440015/restore-tmux-session-after-reboot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment