Skip to content

Instantly share code, notes, and snippets.

@atejeda
Last active August 4, 2019 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atejeda/450658e714ce09bd62b24dc881f1b05d to your computer and use it in GitHub Desktop.
Save atejeda/450658e714ce09bd62b24dc881f1b05d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# used to match output from `tmux list-keys`
KEY_BINDING_REGEX="bind-key[[:space:]]\+\(-r[[:space:]]\+\)\?\(-T prefix[[:space:]]\+\)\?"
is_osx() {
local platform=$(uname)
[ "$platform" == "Darwin" ]
}
iterm_terminal() {
[[ "$TERM_PROGRAM" =~ ^iTerm ]]
}
command_exists() {
local command="$1"
type "$command" >/dev/null 2>&1
}
# returns prefix key, e.g. 'C-a'
prefix() {
tmux show-option -gv prefix
}
# if prefix is 'C-a', this function returns 'a'
prefix_without_ctrl() {
local prefix="$(prefix)"
echo "$prefix" | cut -d '-' -f2
}
option_value_not_changed() {
local option="$1"
local default_value="$2"
local option_value=$(tmux show-option -gv "$option")
[ "$option_value" == "$default_value" ]
}
server_option_value_not_changed() {
local option="$1"
local default_value="$2"
local option_value=$(tmux show-option -sv "$option")
[ "$option_value" == "$default_value" ]
}
key_binding_not_set() {
local key="$1"
if $(tmux list-keys | grep -q "${KEY_BINDING_REGEX}${key}[[:space:]]"); then
return 1
else
return 0
fi
}
key_binding_not_changed() {
local key="$1"
local default_value="$2"
if $(tmux list-keys | grep -q "${KEY_BINDING_REGEX}${key}[[:space:]]\+${default_value}"); then
# key still has the default binding
return 0
else
return 1
fi
}
main() {
# OPTIONS
# enable utf8 (option removed in tmux 2.2)
tmux set-option -g utf8 on 2>/dev/null
# enable utf8 in tmux status-left and status-right (option removed in tmux 2.2)
tmux set-option -g status-utf8 on 2>/dev/null
# address vim mode switching delay (http://superuser.com/a/252717/65504)
if server_option_value_not_changed "escape-time" "500"; then
tmux set-option -s escape-time 0
fi
# increase scrollback buffer size
if option_value_not_changed "history-limit" "2000"; then
tmux set-option -g history-limit 50000
fi
# tmux messages are displayed for 4 seconds
if option_value_not_changed "display-time" "750"; then
tmux set-option -g display-time 4000
fi
# refresh 'status-left' and 'status-right' more often
if option_value_not_changed "status-interval" "15"; then
tmux set-option -g status-interval 5
fi
# required (only) on OS X
if is_osx && command_exists "reattach-to-user-namespace" && option_value_not_changed "default-command" ""; then
tmux set-option -g default-command "reattach-to-user-namespace -l $SHELL"
fi
# upgrade $TERM, tmux 1.9
if option_value_not_changed "default-terminal" "screen"; then
tmux set-option -g default-terminal "screen-256color"
fi
# upgrade $TERM, tmux 2.0+
if server_option_value_not_changed "default-terminal" "screen"; then
tmux set-option -s default-terminal "screen-256color"
fi
# emacs key bindings in tmux command prompt (prefix + :) are better than
# vi keys, even for vim users
tmux set-option -g status-keys emacs
# focus events enabled for terminals that support them
tmux set-option -g focus-events on
# super useful when using "grouped sessions" and multi-monitor setup
if ! iterm_terminal; then
tmux set-window-option -g aggressive-resize on
fi
# DEFAULT KEY BINDINGS
local prefix="$(prefix)"
local prefix_without_ctrl="$(prefix_without_ctrl)"
# if C-b is not prefix
if [ $prefix != "C-b" ]; then
# unbind obsolete default binding
if key_binding_not_changed "C-b" "send-prefix"; then
tmux unbind-key C-b
fi
# pressing `prefix + prefix` sends <prefix> to the shell
if key_binding_not_set "$prefix"; then
tmux bind-key "$prefix" send-prefix
fi
fi
# If Ctrl-a is prefix then `Ctrl-a + a` switches between alternate windows.
# Works for any prefix character.
if key_binding_not_set "$prefix_without_ctrl"; then
tmux bind-key "$prefix_without_ctrl" last-window
fi
# easier switching between next/prev window
if key_binding_not_set "C-p"; then
tmux bind-key C-p previous-window
fi
if key_binding_not_set "C-n"; then
tmux bind-key C-n next-window
fi
# source `.tmux.conf` file - as suggested in `man tmux`
if key_binding_not_set "R"; then
tmux bind-key R run-shell ' \
tmux source-file ~/.tmux.conf > /dev/null; \
tmux display-message "Sourced .tmux.conf!"'
fi
}
main
# default shell
set-option -g default-shell "/bin/bash"
# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
# reload configuration bind
bind r source-file ~/.tmux.conf
# https://github.com/tmux-plugins/tmux-sensible
run-shell ~/.sensible.tmux

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!

#!/bin/bash
# Script for installing tmux on systems where you don't have root access.
# tmux will be installed in $HOME/local/bin.
# It's assumed that wget and a C/C++ compiler are installed.
# exit on error
set -e
# create our directories
mkdir -p $HOME/local $HOME/tmux_tmp
cd $HOME/tmux_tmp
# download source files for tmux, libevent, and ncurses
wget https://github.com/tmux/tmux/releases/download/2.7/tmux-2.7.tar.gz
wget https://github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz
wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-5.9.tar.gz
# extract files, configure, and compile
############
# libevent #
############
tar xvzf libevent-2.0.19-stable.tar.gz
cd libevent-2.0.19-stable
./configure --prefix=$HOME/local --disable-shared
make
make install
cd ..
############
# ncurses #
############
tar xvzf ncurses-5.9.tar.gz
cd ncurses-5.9
./configure --prefix=$HOME/local
make
make install
cd ..
############
# tmux #
############
tar xvzf tmux-2.7.tar.gz
cd tmux-2.7
./configure CFLAGS="-I$HOME/local/include -I$HOME/local/include/ncurses" LDFLAGS="-L$HOME/local/lib -L$HOME/local/include/ncurses -L$HOME/local/include"
CPPFLAGS="-I$HOME/local/include -I$HOME/local/include/ncurses" LDFLAGS="-static -L$HOME/local/include -L$HOME/local/include/ncurses -L$HOME/local/lib" make
cp tmux $HOME/local/bin
cd ..
# cleanup
rm -rf $HOME/tmux_tmp
echo "$HOME/local/bin/tmux is now available. You can optionally add $HOME/local/bin to your PATH."
# Install tmux 2.8 on Centos
# install deps
yum install gcc kernel-devel make ncurses-devel
# DOWNLOAD SOURCES FOR LIBEVENT AND MAKE AND INSTALL
curl -LOk https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
tar -xf libevent-2.1.8-stable.tar.gz
cd libevent-2.1.8-stable
./configure --prefix=/usr/local
make
make install
# DOWNLOAD SOURCES FOR TMUX AND MAKE AND INSTALL
curl -LOk https://github.com/tmux/tmux/releases/download/2.8/tmux-2.8.tar.gz
tar -xf tmux-2.8.tar.gz
cd tmux-2.8
LDFLAGS="-L/usr/local/lib -Wl,-rpath=/usr/local/lib" ./configure --prefix=/usr/local
make
make install
# pkill tmux
# close your terminal window (flushes cached tmux executable)
# open new shell and check tmux version
tmux -V
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment