Skip to content

Instantly share code, notes, and snippets.

@Bubblemelon
Last active January 20, 2020 00:30
Show Gist options
  • Save Bubblemelon/d7cc9a29abf88d46a602597f1e3a08a2 to your computer and use it in GitHub Desktop.
Save Bubblemelon/d7cc9a29abf88d46a602597f1e3a08a2 to your computer and use it in GitHub Desktop.
My tmux config file
## To erase all previous key bindings:
##
## Restart tmux server to reset key bindings via "tmux kill-server",
## then reopen terminal to automatically source this config.
## Otherwise, run "tmux source-file ~/.tmux.conf"
## list existing bindings:
##
## <prefix>:list-keys
## tmux list-keys
## <prefix>?
## Set prefix to Ctrl + a
## default prefix Ctrl + b
##
## Ctrl prefixed with 'C-' or '^'
## bind alias for bind-key
# set-option -g prefix C-a
# unbind-key C-b
# bind-key C-a send-prefix
## reference: http://man7.org/linux/man-pages/man1/tmux.1.html
## Enable mouse mode (tmux 2.1 and above)
## set alias for set-option
set -g mouse on
## Use Vim keybindings in e.g. copy mode
## setw alias for set-window-option
##
## see set-option for flag definition e.g. -g for global session/window
## reference: https://gist.github.com/andreyvit/2921703
setw -g mode-keys vi
########################################################
# TMUX plugins #
########################################################
## <prefix> + I to fetch the plugin and then source
##
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'knakayama/tmux-uptime'
##
## Restore tmux environment after system restart.
set -g @plugin 'tmux-plugins/tmux-resurrect'
## Custom resurrect bindings: https://github.com/tmux-plugins/tmux-resurrect/blob/master/docs/custom_key_bindings.md
## e.g. <prefix> + S
set -g @resurrect-save 'S'
set -g @resurrect-restore 'R'
## Experimental https://github.com/tmux-plugins/tmux-resurrect/blob/master/docs/restoring_shell_history.md
## set -g @resurrect-save-shell-history 'on'
## Saves tmux env every 15mins
set -g @plugin 'tmux-plugins/tmux-continuum'
##
## Opens files and URLs
## See bindings: https://github.com/tmux-plugins/tmux-open#key-bindings
## "o" to open via mouse selection
## "ctrl-o" edit file mode (editor defined by $EDITOR)
## reference: https://github.com/tmux-plugins/tmux-open/issues/27
setenv -g EDITOR 'vim'
set -g @plugin 'tmux-plugins/tmux-open'
## Status bar
set -g status-right '#{cpu_bg_color} #{cpu_icon} #{cpu_percentage} #{uptime}'
## Switch panes using Alt-arrow
##
## Alt (meta) with 'M-'.
##
## -n for without <prefix>
## if -n is specified, it is not necessary to use the prefix key,
## command is bound to key alone
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
## Set display message time to 3000 miliseconds (3s)
## default: 750 milliseconds
## reference: https://superuser.com/a/295744/957200
set-option -g display-time 3000
## Source tmux config
bind C-R source-file ~/.tmux.conf \; \
display-message "tmux.conf sourced!"
## Copy mode: (key bindings are shown via <prefix>:list-keys -t vi-copy)
##
## <prefix> + [
## move with arrow keys or hjkl
## hit "space" bar to start selection || if not "space", then look for "begin-selection" as key binding
## hit "enter" to copy || look for "copy-selection-and-cancel"
## Copied data is saved in TMUX paste buffer, not system clipboard.
## Paste in copy mode: (look for "paste-buffer")
##
## <prefix> + ]
########################################################
# Below is for tmux 2.4 and above #
########################################################
## See http://man7.org/linux/man-pages/man1/tmux.1.html#KEY_BINDINGS
## For explaination of -T <key-table>
## -X is used to send a command into copy mode
## During copy mode,
## when "y" pressed: (requires pressing "q" to exit message for copy mode)
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard' \; \
display-message "Copied to system clipboard."
# bind -T copy-mode-vi Enter send -X copy-pipe-and-cancel "xclip -i -sel p -f | xclip -i -sel c" \;
# display-message "Copied to system clipboard."
## Toggle rectangular copy mode -- similar to vim visual block
bind -T copy-mode-vi 'v' send-keys -X rectangle-toggle
## Override ']' to paste from clipboard
## bind ] run "tmux set-buffer \"$(xclip -o -sel clipboard)\" && tmux paste-buffer"
## *$* To stop jumping to the bottom of the terminal when copying (vi-mode enabled)
##
## Note: if line 110 uncommented, then tmux will still jump to bottom.
unbind -T copy-mode-vi MouseDragEnd1Pane
##
## Example usage: (to use alongside *#* )
## select/highlight area to copy with mouse, release left mouse button and hit "y" to copy to clipboard (then tmux won't jump)
##
## reference: https://github.com/tmux/tmux/issues/140#issuecomment-321144647
## *#* To copy via mouse selection:
## highlighting area and then release mouse click (will jump to the bottom)
##
# bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
## reference: https://unix.stackexchange.com/a/131187/309143
## Does not cause TMUX to jump to the bottom via mouse selection.
## Needs to be used with line 101, see comment with these characters: *$*
##
## To copy via mouse selection:
## hold left mouse button to highlight area, release to stop highlighting, click left mouse button to exit (will not jump to the bottom)
##
bind -T copy-mode-vi MouseDown1Pane select-pane \;\
send-keys -X copy-pipe 'xclip -in -selection clipboard' \;\
send-keys -X clear-selection
## reference: https://www.freecodecamp.org/news/tmux-in-practice-scrollback-buffer-47d5ffa71c93/
## Initialize TMUX Plugin Manager
[cherylfong@localhost ~]$ cat .tmux.conf
## To erase all previous key bindings:
##
## Restart tmux server to reset key bindings via "tmux kill-server",
## then reopen terminal to automatically source this config.
## Otherwise, run "tmux source-file ~/.tmux.conf"
## list existing bindings:
##
## <prefix>:list-keys
## tmux list-keys
## <prefix>?
## Set prefix to Ctrl + a
## default prefix Ctrl + b
##
## Ctrl prefixed with 'C-' or '^'
## bind alias for bind-key
# set-option -g prefix C-a
# unbind-key C-b
# bind-key C-a send-prefix
## reference: http://man7.org/linux/man-pages/man1/tmux.1.html
## Enable mouse mode (tmux 2.1 and above)
## set alias for set-option
set -g mouse on
## Use Vim keybindings in e.g. copy mode
## setw alias for set-window-option
##
## see set-option for flag definition e.g. -g for global session/window
## reference: https://gist.github.com/andreyvit/2921703
setw -g mode-keys vi
########################################################
# TMUX plugins #
########################################################
## <prefix> + I to fetch the plugin and then source
##
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'knakayama/tmux-uptime'
##
## Restore tmux environment after system restart.
set -g @plugin 'tmux-plugins/tmux-resurrect'
## Custom resurrect bindings: https://github.com/tmux-plugins/tmux-resurrect/blob/master/docs/custom_key_bindings.md
## e.g. <prefix> + S
set -g @resurrect-save 'S'
set -g @resurrect-restore 'R'
## Experimental https://github.com/tmux-plugins/tmux-resurrect/blob/master/docs/restoring_shell_history.md
## set -g @resurrect-save-shell-history 'on'
## Saves tmux env every 15mins
set -g @plugin 'tmux-plugins/tmux-continuum'
##
## Opens files and URLs
## See bindings: https://github.com/tmux-plugins/tmux-open#key-bindings
## "o" to open via mouse selection
## "ctrl-o" edit file mode (editor defined by $EDITOR)
## reference: https://github.com/tmux-plugins/tmux-open/issues/27
setenv -g EDITOR 'vim'
set -g @plugin 'tmux-plugins/tmux-open'
## Status bar
set -g status-right '#{cpu_bg_color} #{cpu_icon} #{cpu_percentage} #{uptime}'
## Switch panes using Alt-arrow
##
## Alt (meta) with 'M-'.
##
## -n for without <prefix>
## if -n is specified, it is not necessary to use the prefix key,
## command is bound to key alone
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
## Set display message time to 3000 miliseconds (3s)
## default: 750 milliseconds
## reference: https://superuser.com/a/295744/957200
set-option -g display-time 3000
## Source tmux config
bind C-R source-file ~/.tmux.conf \; \
display-message "tmux.conf sourced!"
## Copy mode: (key bindings are shown via <prefix>:list-keys -t vi-copy)
##
## <prefix> + [
## move with arrow keys or hjkl
## hit "space" bar to start selection || if not "space", then look for "begin-selection" as key binding
## hit "enter" to copy || look for "copy-selection-and-cancel"
## Copied data is saved in TMUX paste buffer, not system clipboard.
## Paste in copy mode: (look for "paste-buffer")
##
## <prefix> + ]
########################################################
# Below is for tmux 2.4 and above #
########################################################
## See http://man7.org/linux/man-pages/man1/tmux.1.html#KEY_BINDINGS
## For explaination of -T <key-table>
## -X is used to send a command into copy mode
## During copy mode,
## when "y" pressed: (requires pressing "q" to exit message for copy mode)
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard' \; \
display-message "Copied to system clipboard."
# bind -T copy-mode-vi Enter send -X copy-pipe-and-cancel "xclip -i -sel p -f | xclip -i -sel c" \;
# display-message "Copied to system clipboard."
## Toggle rectangular copy mode -- similar to vim visual block
bind -T copy-mode-vi 'v' send-keys -X rectangle-toggle
## Override ']' to paste from clipboard
## bind ] run "tmux set-buffer \"$(xclip -o -sel clipboard)\" && tmux paste-buffer"
## *$* To stop jumping to the bottom of the terminal when copying (vi-mode enabled)
##
## Note: if line 110 uncommented, then tmux will still jump to bottom.
unbind -T copy-mode-vi MouseDragEnd1Pane
##
## Example usage: (to use alongside *#* )
## select/highlight area to copy with mouse, release left mouse button and hit "y" to copy to clipboard (then tmux won't jump)
##
## reference: https://github.com/tmux/tmux/issues/140#issuecomment-321144647
## *#* To copy via mouse selection:
## highlighting area and then release mouse click (will jump to the bottom)
##
# bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
## reference: https://unix.stackexchange.com/a/131187/309143
## Does not cause TMUX to jump to the bottom via mouse selection.
## Needs to be used with line 101, see comment with these characters: *$*
##
## To copy via mouse selection:
## hold left mouse button to highlight area, release to stop highlighting, click left mouse button to exit (will not jump to the bottom)
##
bind -T copy-mode-vi MouseDown1Pane select-pane \;\
send-keys -X copy-pipe 'xclip -in -selection clipboard' \;\
send-keys -X clear-selection
## reference: https://www.freecodecamp.org/news/tmux-in-practice-scrollback-buffer-47d5ffa71c93/
## Initialize TMUX Plugin Manager
run -b '~/.tmux/plugins/tpm/tpm'
# To reload tmux config:
# tmux source-file ~/.tmux.conf
# Enable mouse mode (tmux 2.1 and above)
set -g mouse on
# Use Vim keybindings in copy mode
setw -g mode-keys vi
# tmux plugins
# https://github.com/tmux-plugins/tpm
# To fetch plugins:
# ctrl + b (prefix) then, shift + i
#
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'knakayama/tmux-uptime'
# set -g @plugin 'tmux-plugins/tpm'
# set -g @plugin 'tmux-plugins/tmux-sensible'
# set -g @plugin 'tmux-plugins/tmux-yank'
# set -g @plugin 'tmux-plugins/tmux-open'
# set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
# set -g @plugin 'tmux-plugins/tmux-battery'
# set -g @plugin 'tmux-plugins/tmux-copycat'
# set -g @plugin 'tmux-plugins/tmux-pain-control'
# set -g @plugin 'tmux-plugins/tmux-cpu'
# set -g @plugin 'knakayama/tmux-uptime'
# status bar
set -g status-right '#{cpu_bg_color} #{cpu_icon} #{cpu_percentage} #{uptime}'
# Below is for tmux 2.4 and above
# https://github.com/tmux/tmux/issues/599#issuecomment-255842199
bind -T copy-mode-vi Enter send -X copy-pipe-and-cancel "xclip -i -sel p -f | xclip -i -sel c" \; display-message "Copied to system clipboard."
bind -T copy-mode-vi y send -X copy-pipe-and-cancel "xclip -i -sel p -f | xclip -i -sel c" \; display-message "Copied to system clipboard."
# Toggle rectangular copy mode.
bind -T copy-mode-vi 'v' send-keys -X rectangle-toggle
# Bind ']' to paste.
bind ] run "tmux set-buffer \"$(xclip -o -sel clipboard)\" && tmux paste-buffer"
# Because the Gnome Terminal on Ubuntu is broken: http://askubuntu.com/a/507215/413683
set -s set-clipboard off
# Copy on selection (without any additional keystrokes). This behaviour is the
# default for iTerm2 on macOS.
# http://unix.stackexchange.com/q/348913/128489
# http://stackoverflow.com/a/40902312/4694621
# Requires: set-option -s set-clipboard off
# tmux 2.3 and higher might require a different command:
bind -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel 'xclip -selection clipboard -i'
# Initialize TMUX Plugin Manager
run -b '~/.tmux/plugins/tpm/tpm'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment