Skip to content

Instantly share code, notes, and snippets.

@zsimic
Last active August 14, 2023 05:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsimic/03d4ac3cedf40a88f1d5 to your computer and use it in GitHub Desktop.
Save zsimic/03d4ac3cedf40a88f1d5 to your computer and use it in GitHub Desktop.
unbind-key -a
set -g prefix C-b
# Auto scroll with mouse wheel or page up/down, but only if no other process took the screen (such as vi or less)
set -g mouse on
set -s set-clipboard off
bind -n PageUp if-shell -F "#{alternate_on}" "send-keys PageUp" "copy-mode -e; send-keys PageUp"
bind -n PageDown if-shell "test #{pane_in_mode} -gt 0 -o #{alternate_on} -gt 0" "send-keys PageDown"
bind -n WheelUpPane if-shell -F "#{alternate_on}" "send-keys -M" "select-pane -t= \; copy-mode -e \; send-keys -M"
bind -n WheelDownPane if-shell -F "#{alternate_on}" "send-keys -M" "select-pane -t= \; send-keys -M"
# Most commonly used combinations
bind c new-window -c "#{pane_current_path}"
bind d detach-client
bind -n M-` last-window
bind -n M-Left previous-window
bind -n M-Right next-window
bind PageUp copy-mode -u
# Misc, useful mostly when changing tmux conf
bind , command-prompt -I "#W" "rename-window '%%'"
bind ':' command-prompt
bind ? list-keys
bind q display-panes
bind r source-file ~/.tmux.conf \; display-message "Reloaded ~/.tmux.conf"
bind '~' show-messages
bind -n C-S-Insert kill-session -C # Clear alerts
bind t clock-mode
#bind -n C-S-Home send-keys 'cmatrix -u10' Enter # Just for fun
# Pane splitting (I use this very rarely)
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
bind ! break-pane
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
bind -n M-S-Up select-pane -U
bind -n M-S-Down select-pane -D
bind -n M-S-Left select-pane -L
bind -n M-S-Right select-pane -R
bind -n C-S-Up resize-pane -U 2
bind -n C-S-Down resize-pane -D 2
bind -n C-S-Left resize-pane -L 2
bind -n C-S-Right resize-pane -R 2
# Settings
set -g default-terminal screen-256color
set -g history-limit 100000
set -sg escape-time 0 # Do away with esc/alt disambiguation
set -g visual-activity off
set -g status-keys vi
set -g base-index 1
set -g pane-base-index 1
set -g status-interval 60
setw -g alternate-screen on
setw -g xterm-keys on
set -gw xterm-keys on
setw -g mode-keys vi
# Minimalist status bar, current window highlighted, all others dim, git branch and uptime on bottom right
set -g allow-rename on
setw -g automatic-rename off
set -g set-titles off
setw -g monitor-activity on
setw -g aggressive-resize on
set -g status-justify left
set -g status-style none
set -g status-left ""
setw -g window-status-format ' #W '
setw -g window-status-current-format '⊣ #W ⊢'
setw -g window-status-activity-style fg=red,bright
setw -g window-status-style fg=white,dim
setw -g window-status-current-style fg=green,bright
set -g status-right '#(/usr/bin/python3 ~/bin/shrinky.py tmux_status -p"#{pane_current_path}")'
# set -g status-right '#[fg=blue]#(uptime | egrep -o "up ([^,]+)")'
set -g status-right-length 30
#set -ga terminal-overrides ',xterm*:smcup@:rmcup@'
# Unicode delims: ▏▕ «» ‹› ᚜᚛ ⁅⁆ ⊣⊢ ⸡⸠
@zsimic
Copy link
Author

zsimic commented Dec 1, 2015

This is my .tmux.conf.

Guiding principles:

  • Minimal 1-line status bar, 3 colors used to indicate state of each window
    • bright: current window
    • dark red: some activity occurred in inactive window
    • dim: all other inactive windows
  • Works the same on mac or linux (with a mac or PC keyboard), ie: keyboard keys ⌥ Option, Alt, or ⌘ Cmd can be used interchangeably (all referred to as the Meta key in tmux parlance)
  • Meta + Left/Right moves between tmux windows
  • Meta + Shift + Left/Right/Up/Down moves between panes (within same window)
  • CTRL + Shift + Left/Right/Up/Down resizes panes

Make mouse work nicely with tmux on macOS:

Install tmux: brew install tmux

I use iTerm2 + tmux on macOS, with no UI tabs. It works great, including some nice mouse support.
I just start one tmux session, and use tmux windows exclusively. So, I configure iTerm2 in such a way that its own UI stuff get out of the way (at least on the Profile that I use for tmux).

This is roughly my setup in iTerm2 -> Preferences:

  • -> Appearance -> check Hide scrollbars
  • -> Profiles -> your profile -> Terminal tab:
    • Scrollback lines: 0 (this disables scrollback in iTerm2, we want tmux scrollback only)
    • check Save lines to scrollback in alternate screen mode
    • check Enable mouse reporting
    • uncheck Report mouse click and drags (unless you do want clicks passed through too, in that case you'll have to ⌥+click to select text to copy to clipboard)
  • -> Profiles -> your profile -> Keys tab -> select +Esc for left/right ⌥ option key behavior
  • You can use my linux-like keymap for iTerm2

With a tmux.conf similar to mine, you should now be able to scroll using the mouse wheel in tmux (but no scrolling history outside of tmux)

@zsimic
Copy link
Author

zsimic commented Dec 13, 2020

Starting tmux with a default set of tabs

You can easily start tmux with a predefined set of tabs open, with names and current working directory set... I use this for example:

  • in iTerm2 -> Profiles -> your profile -> Send text at start -> /bin/bash ~/bin/start-tmux
  • ~/bin/start-tmux example shown below
#!/bin/bash

[ ! -x "$(command -v tmux)" ] && echo "tmux not installed" && exit 1
[ -n "$TMUX" ] && echo "already in tmux" && exit 1

if ! tmux has-session -t main 2>/dev/null; then
  tmux new-session -d -s main -n first-window
  tmux new-window -n second-window -c ~/
  tmux new-window -n third-window -c ~/
fi
tmux attach-session -t main

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