Skip to content

Instantly share code, notes, and snippets.

@nicola-strappazzon
Last active July 13, 2020 14:24
Show Gist options
  • Save nicola-strappazzon/a09da5c5f5ade7ea99a749da8468cd3e to your computer and use it in GitHub Desktop.
Save nicola-strappazzon/a09da5c5f5ade7ea99a749da8468cd3e to your computer and use it in GitHub Desktop.
Tmux shortcuts, my personal guidelines

tmux shortcuts

My personal guidelines, and only added shortcuts tested by myself.

Install

Amazon Linux 2

# install deps
sudo yum install -y 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.11-stable/libevent-2.1.11-stable.tar.gz
tar -xf libevent-2.1.11-stable.tar.gz
cd libevent-2.1.11-stable
./configure --prefix=/usr/local
make
sudo make install
cd ..

# DOWNLOAD SOURCES FOR TMUX AND MAKE AND INSTALL

curl -LOk https://github.com/tmux/tmux/releases/download/3.1b/tmux-3.1b.tar.gz
tar -xf tmux-3.1b.tar.gz
cd tmux-3.1b
LDFLAGS="-L/usr/local/lib -Wl,-rpath=/usr/local/lib" ./configure --prefix=/usr/local
make
sudo make install

mv /home/ec2-user/tmux-3.1b/tmux /bin/tmux

# pkill tmux
# close your terminal window (flushes cached tmux executable)
# open new shell and check tmux version
tmux -V

Command Line

Start new:

tmux

Start new with session name:

tmux new -s myname

List sessions:

tmux ls

Attach:

tmux attach-session

Attach with id or named:

tmux a -t [#|myname]

kill session:

tmux kill-session -t myname

Keys

By default, command key bindings are prefixed by Ctrl-b. For example, to vertically split a window type Ctrl-b+%. Another way for Ctrl notation is to replace with this symbol ^ (up arrowhead) or C. For example, ⌃-b+% or C-b+%.

Configuration

A user-specific configuration file should be located at ~/.tmux.conf.

Example

This is the content for .tmux.conf config file:

# General
set-option -g default-terminal screen-256color

# Increase the display time of the pane numbers seen
set-option -g display-panes-time 2000

# Increase scrollback buffer size
set-option -g history-limit 10000

# Set inactive/active window styles
set-option -g window-style fg=colour240,bg=colour236
set-option -g window-active-style fg=colour250,bg=black

# Set the pane border colors
set-option -g pane-border-style bg=colour235,fg=colour238
set-option -g pane-active-border bg=colour235,fg=colour238

# Show reloaded message in bright white
set-window-option -g pane-active-border-style bg=colour235,fg=colour238
set-window-option -g pane-border-style bg=colour235,fg=colour238

set-window-option -g xterm-keys on

# Time and date on right-hand side
set-option -g status-right "#[fg=colour245]%d %b #[fg=colour256] %R"
set-option -g status-interval 60

# Status bar
set-option -g status-bg colour235
set-option -g status-fg colour208

# Windows list
#setw -g mode-bg colour235
#setw -g mode-fg colour208

# Configure Status Info
set-option -g status-right '#(cd #{pane_current_path}; git rev-parse --abbrev-ref HEAD) | #H | %d/%m/%Y %H:%M '
set-option -g status-right-length 50

Reloading config

This can be done either from within tmux, by pressing Ctrl+B and then : to bring up a command prompt, and typing:

source-file ~/.tmux.conf

Key bindings

By default prefix binding of Ctrl-b can be changed to Ctrl-a by adding the following commands in your configuration file:

unbind C-b
set -g prefix C-a
bind C-a send-prefix

Enable Mouse

For enable mouse support.

set -g mouse on

Misc

Ctrl-b+d  detach
Ctrl-b+t  big clock
Ctrl-b+?  list shortcuts
Ctrl-b+:  prompt

Windows

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

Panes

Panes is the split into windows.

Ctrl-b+%    vertical split
Ctrl-b+"    horizontal split
Ctrl-b+q    show pane numbers and press key number to go to pane.
Ctrl-b+x    kill pane

Switching between panes:

Ctrl-b+o      swap panes
Ctrl-b+left   go to the next pane on the left
Ctrl-b+right  (or one of these other directions)
Ctrl-b+up     (or one of these other directions)
Ctrl-b+down   (or one of these other directions)
Ctrl-b+;      go to the ‘last’ (previously used)

Resize pane:

Ctrl-b+Alt-Up      resize to Up
Ctrl-b+Alt-Down    resize to Down
Ctrl-b+Alt-Left    resize to Left
Ctrl-b+Alt-Right   resize to Right
Ctrl-b+z           Maximize / Minimize

Navigation:

Ctrl-b+[   Enter into Copy/Scroll mode, press q or Esc to quit.
           - Use the arrow key to move by line by line.
           - Press Ctrl-SPACE to start copying.
           - Press ALT-w or Ctrl-w to copy into Tmux buffer. Or press Enter on Mac OS's.
           - Press Ctrl-b+] to paste in a possibly different Tmux pane/window.
           - To move line by line with stop cursor use the combination Ctrl-[Up|Down] or Shift-[k|j].
           - In MAC to move page by page use Fn-[Up|Down].

Search:

Where you are into Copy/Scroll mode Ctrl-b+[, press Ctrl-s then type the string to search for and press Enter. Press n to search for the same string again. Press Shift-n for reverse search. Press q to exit.

Nested sessions:

You have tmux on local environment, and connet via SSH to bastion server and them start tmux session there. This is called nested sessions.

Laptop (tmux) ---ssh---> Bastion (tmux)

The most common solution is to press prefix twice (prefix is a keybinding that puts tmux in a command mode, usually it's Ctrl-b, but some people prefer remapping it to screen-like Ctrl-a).

The commands that you type in your terminal are first handled by your local tmux instance, then handled by the nested tmux session. This means you need to escape twice to go to the nested instance:

For example, to deatach from tmux on server, press Ctrl-b-b+d.

Dependencies

Dependencies? Yes, On recent version of Mac OS's greater than 10.5 implement peculiarities security, as process and daemons haven’t access to user-level services like the pasteboard since OS. There’s a library called reattach-to-user-namespace that uses some private operating system functions to get tmux access to the pasteboard.

brew install reattach-to-user-namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment