Skip to content

Instantly share code, notes, and snippets.

@adriancmiranda
Forked from MohamedAlaa/tmux-cheatsheet.markdown
Last active February 23, 2023 10:10
Show Gist options
  • Save adriancmiranda/06f48ffd0108c0646f97 to your computer and use it in GitHub Desktop.
Save adriancmiranda/06f48ffd0108c0646f97 to your computer and use it in GitHub Desktop.
tmux shortcuts & cheatsheet
# ~/.tmux.conf
###########################################################################
# Change prefix from default (C-b) to C-g
# C-g doesn't interfere with emacs-style editing keys
unbind -n C-b
unbind -n C-g
set -g prefix C-g
# C-g C-g to passthrough a C-g
unbind C-g
bind C-g send-key C-g
###########################################################################
# General options
# Default termtype. If the rcfile sets $TERM, that overrides this value.
set -g default-terminal screen-256color
# Ring the bell if any background window rang a bell
set -g bell-action any
# Watch for activity in background windows
setw -g monitor-activity on
# scrollback size
set -g history-limit 10000
# set first window to index 1 (not 0) to map more to the keyboard layout
set -g base-index 1
setw -g pane-base-index 1
# pass through xterm keys
set -g xterm-keys on
###########################################################################
# General keymap
# Keep your finger on ctrl, or don't, same result
bind-key C-d detach-client
bind-key C-p paste-buffer
# Redraw the client (if interrupted by wall, etc)
bind R refresh-client
# reload tmux config
unbind r
bind r \
source-file ~/.tmux.conf \;\
display 'Reloaded tmux config.'
# Use emacs keybindings for tmux commandline input.
# (Note: if in vi mode, to get command mode you need to hit ESC twice.)
set -g status-keys emacs
# Use vi keybindings in copy and choice modes
setw -g mode-keys vi
###########################################################################
# Window management / navigation
# move between windows
bind-key C-h previous-window
bind-key C-l next-window
# C-\ (no prefix) to skip to last window we were in before this one
bind -n "C-\\" last-window
# C-Space (no prefix) to tab to next window
bind -n C-Space next-window
###########################################################################
# Pane management / navigation
# Horizontal splits with s or C-s
unbind s
unbind C-s
bind-key s split-window
bind-key C-s split-window
# Vertical split with v or C-v
unbind v
unbind C-v
bind-key v split-window -h
bind-key C-v split-window -h
# navigation with C-{h,j,k,l} -- NO PREFIX
# https://gist.github.com/mislav/5189704
bind -n C-k run-shell 'tmux-vim-select-pane -U'
bind -n C-j run-shell 'tmux-vim-select-pane -D'
bind -n C-h run-shell 'tmux-vim-select-pane -L'
bind -n C-l run-shell 'tmux-vim-select-pane -R'
# C-g C-k to passthrough a C-k
# C-k is consumed for pane navigation but we want it for kill-to-eol
unbind C-k
bind C-k send-key C-k
# Pane resize in all four directions using vi bindings.
# Can use these raw but I map them to Cmd-Opt-<h,j,k,l> in iTerm2.
# http://tangledhelix.com/blog/2012/04/28/iterm2-keymaps-for-tmux/
# Note on a few prefix options: C-a = 0x01, C-b = 0x02, C-g = 0x06
bind-key J resize-pane -D
bind-key K resize-pane -U
bind-key H resize-pane -L
bind-key L resize-pane -R
# easily toggle synchronization (mnemonic: e is for echo)
# sends input to all panes in a given window.
bind e setw synchronize-panes on
bind E setw synchronize-panes off
###########################################################################
# Scrollback / pastebuffer
# Vim-style copy/paste
unbind [
bind y copy-mode
unbind p
bind p paste-buffer
bind -t vi-copy v begin-selection
bind -t vi-copy y copy-selection
bind -t vi-copy Escape cancel
###########################################################################
# Mouse mode is on by default.
# <prefix> M -- to turn it off
# <prefix> m -- to turn it on
#
# As of tmux 2.1, a backward-incompatible change was introduced.
# mode-mouse and mouse-* no longer exist, replaced by simply
# mouse <on|off>. Which is great, and easier, but unfortunately I use
# tmux on systems which I don't foresee going to 2.1+ anytime soon.
# So, time to test versions... this is kind of cheap and hacky, and it
# won't work for 2.2 and beyond, but will work for now. I tried to make
# this more generalized but have not been successful so far.
#
if-shell '[[ $(echo "$(tmux -V | cut -d" " -f2) >= 2.1" | bc) -eq 1 ]]' \
'set -g mouse on; bind m set -g mouse on \; display "Mouse ON"; bind M set -g mouse off \; display "Mouse OFF"' \
'set -g mode-mouse on; set -g mouse-resize-pane on; set -g mouse-select-pane on; set -g mouse-select-window on; bind m set -g mode-mouse on \; set -g mouse-resize-pane on \; set -g mouse-select-pane on \; set -g mouse-select-window on \; display "Mouse ON"; bind M set -g mode-mouse off \; set -g mouse-resize-pane off \; set -g mouse-select-pane off \; set -g mouse-select-window off \; display "Mouse OFF"'
###########################################################################
# Color scheme (Solarized light)
### default statusbar colors
##set-option -g status-bg colour231 #base2
##set-option -g status-fg colour130 #yellow
##set-option -g status-attr default
##
### default window title colors
##set-window-option -g window-status-fg colour180 #base00
##set-window-option -g window-status-bg default
##
### active window title colors
##set-window-option -g window-status-current-fg colour196 #orange
##set-window-option -g window-status-current-bg default
##
### pane border
##set-option -g pane-active-border-fg black
##set-option -g pane-active-border-bg white
##set-option -g pane-border-fg brightcyan
##set-option -g pane-border-bg default
##
### message text
##set-option -g message-bg colour231 #base2
##set-option -g message-fg colour196 #orange
##
### pane number display
##set-option -g display-panes-active-colour colour20 #blue
##set-option -g display-panes-colour colour196 #orange
##
### clock
##set-window-option -g clock-mode-colour colour40 #green
###########################################################################
# Color scheme (Solarized Dark)
### default statusbar colors
##set-option -g status-bg brightgreen
##set-option -g status-fg white
##set-option -g status-attr default
##
### default window title colors
##set-window-option -g window-status-fg brightcyan
##set-window-option -g window-status-bg default
###set-window-option -g window-status-attr dim
##
### active window title colors
##set-window-option -g window-status-current-fg white
##set-window-option -g window-status-current-bg blue
###set-window-option -g window-status-current-attr bright
##
### pane border
##set-option -g pane-border-fg brightgreen
##set-option -g pane-active-border-fg red
##
### message text
##set-option -g message-bg black
##set-option -g message-fg red
##
### pane number display
##set-option -g display-panes-active-colour red
##set-option -g display-panes-colour white
##
### clock
##set-window-option -g clock-mode-colour cyan
###########################################################################
# Color scheme (Tango Dark)
## default statusbar colors
#set-option -g status-bg blue
#set-option -g status-fg white
#
## active window title colors
#set-window-option -g window-status-current-fg yellow
#set-window-option -g window-status-current-bg black
#
## pane border
#set-option -g pane-border-fg blue
#set-option -g pane-active-border-fg red
#
## pane number display
#set-option -g display-panes-active-colour red
#set-option -g display-panes-colour blue
###########################################################################
# Color scheme (Cobalt)
# default statusbar colors
set-option -g status-fg white
set-option -g status-bg cyan
# active window title colors
set-window-option -g window-status-current-fg red
set-window-option -g window-status-current-bg yellow
# pane border
set-option -g pane-border-fg cyan
set-option -g pane-active-border-fg red
# pane number display
set-option -g display-panes-active-colour red
set-option -g display-panes-colour cyan
###########################################################################

Basic tmux Tutorial - Windows, Panes, and Sessions over SSH

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname

attach:

tmux a  #  (or at, or attach)

attach to named:

tmux a -t myname

list sessions:

tmux ls

kill session:

tmux kill-session -t myname

Kill all the tmux sessions:

tmux ls | grep : | cut -d. -f1 | awk '{print substr($1, 0, length($1)-1)}' | xargs kill

In tmux, hit the prefix ctrl+b (my modified prefix is ctrl+a) and then:

Sessions

:new<CR>  new session
s  list sessions
$  name session

Windows (tabs)

c  create window
w  list windows
n  next window
p  previous window
f  find window
,  name window
&  kill window

Panes (splits)

%  vertical split
"  horizontal split

o  swap panes
q  show pane numbers
x  kill pane
+  break pane into window (e.g. to select text by mouse to copy)
-  restore pane from window
⍽  space - toggle between layouts
<prefix> q (Show pane numbers, when the numbers show up type the key to goto that pane)
<prefix> { (Move the current pane left)
<prefix> } (Move the current pane right)
<prefix> z toggle pane zoom

Sync Panes

You can do this by switching to the appropriate window, typing your Tmux prefix (commonly Ctrl-B or Ctrl-A) and then a colon to bring up a Tmux command line, and typing:

:setw synchronize-panes

You can optionally add on or off to specify which state you want; otherwise the option is simply toggled. This option is specific to one window, so it won’t change the way your other sessions or windows operate. When you’re done, toggle it off again by repeating the command. tip source

Resizing Panes

You can also resize panes if you don’t like the layout defaults. I personally rarely need to do this, though it’s handy to know how. Here is the basic syntax to resize panes:

PREFIX : resize-pane -D (Resizes the current pane down)
PREFIX : resize-pane -U (Resizes the current pane upward)
PREFIX : resize-pane -L (Resizes the current pane left)
PREFIX : resize-pane -R (Resizes the current pane right)
PREFIX : resize-pane -D 20 (Resizes the current pane down by 20 cells)
PREFIX : resize-pane -U 20 (Resizes the current pane upward by 20 cells)
PREFIX : resize-pane -L 20 (Resizes the current pane left by 20 cells)
PREFIX : resize-pane -R 20 (Resizes the current pane right by 20 cells)
PREFIX : resize-pane -t 2 20 (Resizes the pane with the id of 2 down by 20 cells)
PREFIX : resize-pane -t -L 20 (Resizes the pane with the id of 2 left by 20 cells)

Copy mode:

Pressing PREFIX [ places us in Copy mode. We can then use our movement keys to move our cursor around the screen. By default, the arrow keys work. we set our configuration file to use Vim keys for moving between windows and resizing panes so we wouldn’t have to take our hands off the home row. tmux has a vi mode for working with the buffer as well. To enable it, add this line to .tmux.conf:

setw -g mode-keys vi

With this option set, we can use h, j, k, and l to move around our buffer.

To get out of Copy mode, we just press the ENTER key. Moving around one character at a time isn’t very efficient. Since we enabled vi mode, we can also use some other visible shortcuts to move around the buffer.

For example, we can use "w" to jump to the next word and "b" to jump back one word. And we can use "f", followed by any character, to jump to that character on the same line, and "F" to jump backwards on the line.

   Function                vi             emacs
   Back to indentation     ^              M-m
   Clear selection         Escape         C-g
   Copy selection          Enter          M-w
   Cursor down             j              Down
   Cursor left             h              Left
   Cursor right            l              Right
   Cursor to bottom line   L
   Cursor to middle line   M              M-r
   Cursor to top line      H              M-R
   Cursor up               k              Up
   Delete entire line      d              C-u
   Delete to end of line   D              C-k
   End of line             $              C-e
   Goto line               :              g
   Half page down          C-d            M-Down
   Half page up            C-u            M-Up
   Next page               C-f            Page down
   Next word               w              M-f
   Paste buffer            p              C-y
   Previous page           C-b            Page up
   Previous word           b              M-b
   Quit mode               q              Escape
   Scroll down             C-Down or J    C-Down
   Scroll up               C-Up or K      C-Up
   Search again            n              n
   Search backward         ?              C-r
   Search forward          /              C-s
   Start of line           0              C-a
   Start selection         Space          C-Space
   Transpose chars                        C-t

Misc

d  detach
t  big clock
?  list shortcuts
:  prompt

Configurations Options:

# Mouse support - set to on if you want to use the mouse
* setw -g mode-mouse off
* set -g mouse-select-pane off
* set -g mouse-resize-pane off
* set -g mouse-select-window off

# Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"

# enable activity alerts
setw -g monitor-activity on
set -g visual-activity on

# Center the window list
set -g status-justify centre

# Maximize and restore a pane
unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp
unbind Down
bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp

Resources:

Notes:

Changelog:

Request an Update:

We Noticed that our Cheatsheet is growing and people are coloberating to add new tips and tricks, so please tweet to me what would you like to add and let's make it better!

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