Skip to content

Instantly share code, notes, and snippets.

@OrangeCrush
Last active December 21, 2019 11:49
Show Gist options
  • Save OrangeCrush/3a501a6d027a6fb54dbe to your computer and use it in GitHub Desktop.
Save OrangeCrush/3a501a6d027a6fb54dbe to your computer and use it in GitHub Desktop.
Tmux Notes

Tmux 101

Why you should use tmux

  • Detachable for long running scripts / workloads
  • Organize your work
  • Remember what you were working on last
  • Type in your password less
  • Keep your ssh sessions alive when transporting laptop
  • Sync panes to work in multiple at once
  • Look like a hacker

Installation

Packaged by RedHat on EL7

  • See instructions below for compiling on EL6 (without EPEL)
# yum install -y tmux

Getting Started

Create a new session, with 1 window, and 1 pane

$ tmux

The story of Sessions, Windows, Panes

Sessions

Sessions are tmux objects that contain windows (and panes). Think of them as chapters in a book. Sessions can be created by starting tmux at the command line or from inside tmux.

## Manipulating Sessions ##
<C-b> :new-session   # Create a new tmux session
<C-b> s              # Switch between sessions
<C-b> $              # Enter a new name for this session

Windows

Windows are the pages inside of each chapter. Many windows can be inside of 1 session. A window holds one or more panes.

## Manipulating Windows ##
<C-b> c        # Create a new window
<C-b> n        # Move to the next window
<C-b> p        # Move to the previous window
<C-b> ,        # Rename the current window
<C-b> w        # View and move between windows interactively
<C-b> [0-9]+   # Switch to window N

Panes

Panes are the paragraphs and words on each page. One window can have many Panes. Panes are the most interactive objects in tmux, and run your shell.

## Manipulating Panes ##
<C-b> %         # Split the Pane vertically,   Creating a new pane
<C-b> "         # Split the Pane horizontally, Creating a new pane
<C-b> <arrow>   # Change panes by direction
<C-b> q         # Change panes by number
<C-b> <space>   # Reorganize panes
<C-b> {         # Rotate pane left
<C-b> }         # Rotate pane right

Misc Useful commands

<C-b> z                         # Zoom in/out of a pane
<C-b> :setw synchronize-panes   # Type in all panes in the current window at once
<C-b> d                         # Detach from the current session
<C-b> x                         # Kill an unresponsive pane
<C-b> ?                         # Panic mode. Show all commands + their bindings

# Reattach to last tmux session
$ tmux attach

Copy Mode

One drawback of the software is that traditional scrolling is not going to work very well. (This can probably be configured) Copy mode can fix this issue and keep thousands of lines in your sessions history.

## Outside of copy mode
<C-b> [     # Enter copy mode (Similar to vim :visual mode)

## Inside copy mode
<arrow keys> # Navigate copy mode
'v'          # Highlight text
'enter'      # Copy selected text into a paste buffer
'q'          # Leave copy mode

## Outside of copy mode
<C-b> ]  # Paste the copied text into the current pane

Configuration (~/.tmux.conf)

Some of the commands are a bit of a mouthful so here's my personal recommended bare bones ~/.tmux.conf

# Bind new pane creation to | and _
unbind-key %
bind | split-window -h
unbind-key '"'
bind _ split-window -v

## Move between panes with vim bindings
unbind-key j
bind-key j select-pane -D
unbind-key k
bind-key k select-pane -U
unbind-key h
bind-key h select-pane -L
unbind-key l
bind-key l select-pane -R

## Bind C-b to synchronize-panes
bind-key C-b setw synchronize-panes

# New session
unbind-key N
bind-key N new-session

Working with AIX

Change the TERM environment variable in order to properly run common AIX commands

$ topas
Terminal screen is unknown.

# Fix unknown terminal screen errors
$ export TERM=vt100

Scripting tmux

One of the most useful features of tmux is it's api. Almost any of the above commands can be wrapped up into a script.

Here's an example taking a list of servers, creating a session, window and panes for each server, and storing the hostname in a $SERVER variable

#!/bin/bash

#
# I like to run this like
# cat serverfile | xargs ./panes.sh name-of-pane
#
NAME=$1
shift

if [ $# == 0 ]
then
   echo "give params"
   exit 127
fi

tmux new-session -d -s $NAME
tmux send-keys "SERVER=$1"
shift
for PANE in $@
do
   tmux split-window -h
   tmux send-keys "SERVER=${PANE}"
   tmux next-layout
done

Compiling tmux on RHEL6

  1. Download the tmux source code
  2. Download the libevent-2.x stable release
## Install deps that are packaged for RHEL6
yum install automake libtool ncurses-devel

## Compiling libevent
$ ./autogen.sh
$ ./configure && make
# make install
# ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5

## Compiling tmux
$ ./autogen.sh
$ ./configure && make
# make install


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