Skip to content

Instantly share code, notes, and snippets.

@jbritton
Last active June 27, 2018 16:06
Show Gist options
  • Save jbritton/2f94014bb7bd39a9a574ad91c1aa66d1 to your computer and use it in GitHub Desktop.
Save jbritton/2f94014bb7bd39a9a574ad91c1aa66d1 to your computer and use it in GitHub Desktop.

tmux

Overview

  • tmux is a great solution for managing the state of terminal sessions. This allows you to jump between terminal sessions with ease so you can multitask while not losing your work. In addition, the window and pane display functionality allows you to create dashboard-like layouts for your terminal sessions.
  • tmux is based around sessions. Sessions can be created, attached, and detached. This allows you to create sessions, detach from them, and reattach to them to pick up where you left off.
  • tmux sessions have a zero-indexed number associated with them. This serves as a identifier that's used to reattach to an existing session. They can also be named to give context.
  • tmux windows are contained within a session. A session can have one or many windows. A window is a grouping of panes. Like sessions, windows are also numbered or nameable.
  • tmux panes can be used to run commands, monitor processes, tail logs, etc.

Sessions

List existing tmux sessions:

$> tmux ls

Create a new session:

$> tmux new

Exiting a session. This will terminate the current tmux session:

$> exit

Kill all tmux sessions:

$> tmux kill-server

Attach, Detach & Kill

Using the prefix shortcut. Type ctrl+b and then run a tmux command or type : to get a tmux prompt.

Detaching from the current tmux session. This will return you to your normal shell without terminating the tmux session.

$> ctrl+b
$> d

Reattaching to a session using its identifier:

$> tmux attach-session -t 3

$> tmux a -t 3

Reattaching to the last created session:

$> tmux a #

Killing a session:

$> tmux kill-session -t 3

Naming Sessions

Create a named session:

$> tmux new -s [name of session]

$> tmux new -s example-session

Rename a session:

$> ctrl+b $
$> another-session

Attaching to a named session:

$> tmux a -t [name of session]

$> tmux a -t example-session

Panes

Split a pane horizontally:

$> ctrl+b "

Split a pane vertically:

$> ctrl+b %

Navigate between panes:

$> ctrl+b [arrow key]

Resizing Panes

Resize a pane. Use direction flags: -U for up, -D for down, -L for left, -R for right:

$> ctrl+b :
$> resize-pane -D

Resize a pane, specifying a direction and number of lines:

$> ctrl+b :
$> resize-pane -R 5

Kill the current pane:

$> ctrl+b x

Windows

Creating a new window:

$> ctrl+b c

Killing a window:

$> ctrl+b &

Navigating to the next window:

$> ctrl+b n

Navigating to the previous window:

$> ctrl+b p

Renaming a window:

$> ctrl+b ,
$> example-window

Configuration

Create a tmux configuration file:

$> touch ~/.tmux.conf

Example configurations:


# send prefix
set-option -g prefix C-a
unbind-key C-a
bind-key C-a send-prefix

# use alt-arrow keys to switch panes
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

# use shift-arrow keys to switch windows
bind -n S-Left previous-window
bind -n S-Right next-window

# mouse mode
set -g mode-mouse on
set -g mouse-resize-pain on
set -g mouse-select-pane on
set -g mouse-select-window on

# set easier window split keys
bind-key v split-window -h
bind-key h split-window -v

# easy config reload
bind-key r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"

Resources

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