Skip to content

Instantly share code, notes, and snippets.

@lucian
Last active January 13, 2022 09:11
Show Gist options
  • Save lucian/98d0cc4e7ea235e41f605d8ccaa3f23c to your computer and use it in GitHub Desktop.
Save lucian/98d0cc4e7ea235e41f605d8ccaa3f23c to your computer and use it in GitHub Desktop.
tmux

Tmux Workshop

What is a Terminal

TTY - TeleTYpe

The TTY (TeleTYpe), TDD (Telecommunications Device for the Deaf), and TT (Text Telephone) acronyms are used interchangeably to refer to any type of text-based telecommunications equipment used by a person who does not have enough functional hearing to understand speech, even with amplification. The person sending a message types it on a small keyboard, and the person receiving the message reads the text on the display. In order for a person to use a text telephone, the individual at the other end of the conversation must also have one, or they must use a relay service. A relay service provides an operator who reads the text on a TTY and speaks the message into a telephone and/or listens on a telephone and types the message on a TTY.

TTY

Why we need a Terminal

Terminals

TTY Demystified

VT100.net

  • VT05 - DEC 1970

  • VT52 - DEC 1974

  • VT100 - DEC 1978

  • VT220 - DEC 1983

  • Teletype Model 33 - 1963 - Bill Gate Teletype Model 33

Fun fact: Bill Gates cites the Teletype Model 33 as his first computing experience. In 1973 Gates’ Lakeside Prep School Mothers Club used its garage sale proceeds to give students computer access. The club purchased a Model-33 teletype machine that connected over phone lines to a GE time sharing computer.

Console.dev - When computers had personality

Valid ANSI Mode Control Sequences

CPR – Cursor Position Report – VT100 to Host
CUB – Cursor Backward – Host to VT100 and VT100 to Host
CUD – Cursor Down – Host to VT100 and VT100 to Host
CUF – Cursor Forward – Host to VT100 and VT100 to Host
CUP – Cursor Position
CUU – Cursor Up – Host to VT100 and VT100 to Host
DA – Device Attributes
DECALN – Screen Alignment Display (DEC Private)
DECANM – ANSI/VT52 Mode (DEC Private)
DECARM – Auto Repeat Mode (DEC Private)
DECAWM – Autowrap Mode (DEC Private)
DECCKM – Cursor Keys Mode (DEC Private)
DECCOLM – Column Mode (DEC Private)
DECDHL – Double Height Line (DEC Private)
DECDWL – Double-Width Line (DEC Private)
DECID – Identify Terminal (DEC Private)
DECINLM – Interlace Mode (DEC Private)
DECKPAM – Keypad Application Mode (DEC Private)
DECKPNM – Keypad Numeric Mode (DEC Private)
DECLL – Load LEDS (DEC Private)
DECOM – Origin Mode (DEC Private)
DECRC – Restore Cursor (DEC Private)
DECREPTPARM – Report Terminal Parameters
DECREQTPARM – Request Terminal Parameters
DECSC – Save Cursor (DEC Private)
DECSCLM – Scrolling Mode (DEC Private)
DECSCNM – Screen Mode (DEC Private)
DECSTBM – Set Top and Bottom Margins (DEC Private)
DECSWL – Single-width Line (DEC Private)
DECTST – Invoke Confidence Test
DSR – Device Status Report
ED – Erase In Display
EL – Erase In Line
HTS – Horizontal Tabulation Set
HVP – Horizontal and Vertical Position
IND – Index
LNM – Line Feed/New Line Mode
NEL – Next Line
RI – Reverse Index
RIS – Reset To Initial State
RM – Reset Mode
SCS – Select Character Set
SGR – Select Graphic Rendition
SM – Set Mode
TBC – Tabulation Clear

[Linux console](https://en.wikipedia.org/wiki/Linux_console "The Linux console was one of the first features of the kernel and was originally written by Linus Torvalds in 1991" History of Linux

Parser for escape and control sequences

state machine for a parser for escape and control sequences, suitable for use in a VT emulator

A parser for DEC’s ANSI-compatible video terminals

Why DEC-compatible, not just ANSI-compatible?

All of DEC’s terminals from the VT100 onward are compatible with
ANSI X3.64-1979, “Additional Controls for Use with American National
Standard Code for Information Interchange”, hereafter referred to just
as X3.64. However, X3.64 defines many implementation-dependent features
and error conditions without defining recovery procedures. A sample of
these is given below; a more detailed treatment appears later.

- Occurrences of characters 00-1F or 7F-FF in an escape sequence or control
  sequence is an error condition whose recovery is not specified.

- For control sequences, the maximum length of parameter string is defined
  by implementation.

- For control sequences, occurrences of a parameter character after
  an intermediate character is an error condition.

Terminal Emulators

Terminal Emulators

  • xterm - standard terminal for X11 (X Window System)
  • PuTTY
  • iTerm2
  • Alacritty

Terminal emulators that can run inside other terminals

  • tmux
  • GNU screen

Working in a Terminal

  • conection - local or ssh
  • TERM env
  • colors, fonts, history
msgcat --color=test
awk 'BEGIN{
    s="/\\/\\/\\/\\/\\"; s=s s s s s s s s;
    for (colnum = 0; colnum<77; colnum++) {
        r = 255-(colnum*255/76);
        g = (colnum*510/76);
        b = (colnum*255/76);
        if (g>255) g = 510-g;
        printf "\033[48;2;%d;%d;%dm", r,g,b;
        printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
        printf "%s\033[0m", substr(s,colnum+1,1);
    }
    printf "\n";
}'

Without tmux

  • open multiple terminals (windows/tabs)
  • login on ssh remotely
  • close the session
  • reconnect

with tmux

  • one terminal - multiple connection
  • use only the keyboard
  • session not lost

Tmux

Overview

Summary of terms

Client	        - Attaches a tmux session from an outside terminal such as
                  xterm(1)
Session	        - Groups one or more windows together
Window	        - Groups one or more panes together, linked to one or more
                  sessions
Pane	          - Contains a terminal and running program, appears in one
                  window
Active pane	    - The pane in the current window where typing is sent; one per 
                  window
Current window	- The window in the attached session where typing is sent;
                  one per session
Last window	    - The previous current window
Session name	  - The name of a session, defaults to a number starting from
                  zero
Window list	     - The list of windows in a session in order by number
Window name	     - The name of a window, defaults to the name of the running
                   program in the active pane
Window index	   - The number of a window in a session's window list
Window layout	   - The size and position of the panes in a window

Sessions, Windows ad Panes

Tactical tmux: The 10 Most Important Commands

Sessions

  • new session
$ tmux new -s session-name
  • detach from a session
ctrl-b-d
  • show existing sessions
$ tmux ls
  • attach to an esisting session
$ tmux attach -t 0
$ tmux attach -t session-name
  • killing a session
$ tmux kill-session -t session-name

Windows and Panes

+----------------------------------------------------+
| SESSION                                            |
| +-------------------------+ +---------------------+|
| | WINDOW                  | | WINDOW              ||
| |+-----------------------+| |                     ||
| || PANE                  || |                     ||
| ||                       || |                     ||
| ||                       || |                     ||
| ||                       || |                     ||
| ||                       || |                     ||
| |+-----------------------+| |                     ||
| |+-----------------------+| |                     ||
| || PANE                  || |                     ||
| ||                       || |                     ||
| ||                       || |                     ||
| ||                       || |                     ||
| ||                       || |                     ||
| |+-----------------------+| |                     ||
| +-------------------------+ +---------------------+|
+----------------------------------------------------+

A quick command reference

insdie tmux 'ctrl-b' shourctcut

Basics
 - ? get help
 
 Session Management
 - s list sessions
 - $ rename the current session
 - d detach from the current session
 
 Windows
 - c create a new window
 - , rename the current window
 - w list windows
 - % split horizontally
 - " split vertically
 - n change to the next window
 - p change to the previous window
 - 0 to 9 select windows 0 through 9
 
 Panes
- % create a horizontal pane
- " create a vertical pane
- h move to the left pane. *
- j move to the pane below *
- l move to the right pane *
- k move to the pane above *
- q show pane numbers
- o toggle between panes
- } swap with next pane
- { swap with previous pane
- ! break the pane out of the window
- x kill the current pane

Copy & Paste

  • default behavior
- Enter ‘copy mode’ by pressing CTRL+b, [.

- Use the arrow keys to go to the position from where you want to start    
  copying. Press CTRL+SPACE to start copying.
  
- Use arrow keys to go to the end of text you want to copy. 
  Press ALT+w or CTRL+w to copy into Tmux buffer.
  
- Press CTRL+b, ] to paste in a possibly different Tmux pane/window.

tmux Windows and Panes

  • emacs mode default
Ctrl-b
Ctrl-a
Ctrl-e
Ctrl-p
Ctrl-n
  • vi mode

https://tmuxcheatsheet.com - Copy Mode

Ctrl-b :
:setw -g mode-keys vi

Advanced - configuration, scripting, plugins

configration

!! Use default config / shortcuts - easy to use on a fresh setup

  • on a remote system - the only config: ~/.tmux.conf
set-option -g history-limit 100000
  • on the local system
set -g prefix C-z
unbind C-b

set-option -g history-limit 100000

scripting

Bash scripts with tmux to launch a 4-paned window

#!/bin/sh

tmux new-session -d -s foo 'exec pfoo'
tmux send-keys 'bundle exec thin start' 'C-m'
tmux rename-window 'Foo'
tmux select-window -t foo:0
tmux split-window -h 'exec pfoo'
tmux send-keys 'bundle exec compass watch' 'C-m'
tmux split-window -v -t 0 'exec pfoo'
tmux send-keys 'rake ts:start' 'C-m'
tmux split-window -v -t 1 'exec pfoo'
tmux -2 attach-session -t foo

Scripting A Tmux Work-space Start-up

#!/bin/sh

# Set Session Name
SESSION="Website"
SESSIONEXISTS=$(tmux list-sessions | grep $SESSION)

# Only create tmux session if it doesn't already exist
if [ "$SESSIONEXISTS" = "" ]
then
    # Start New Session with our name
    tmux new-session -d -s $SESSION

    # Name first Pane and start zsh
    tmux rename-window -t 0 'Main'
    tmux send-keys -t 'Main' 'zsh' C-m 'clear' C-m # Switch to bind script?

    # Create and setup pane for hugo server
    tmux new-window -t $SESSION:1 -n 'Hugo Server'
    tmux send-keys -t 'Hugo Server' 'hugo serve -D -F' C-m # Switch to bind script?

    # setup Writing window
    tmux new-window -t $SESSION:2 -n 'Writing'
    tmux send-keys -t 'Writing' "nvim" C-m

    # Setup an additional shell
    tmux new-window -t $SESSION:3 -n 'Shell'
    tmux send-keys -t 'Shell' "zsh" C-m 'clear' C-m
fi

# Attach Session, on the Main window
tmux attach-session -t $SESSION:0

plugins

tmux-plugins

  • tmux-resurrect - persists
    tmux environment across system restarts
  • tpm - tmux plugin manager

internals

others

$ tmux new-session -s shared "tmux new-window -n irc irssi"

$ tmux link-window -s <src-window> -t <dst-window>

$ tmux link-window -s shared:irc -t 9
  • shared sessions for code review - tmate

Shortcuts

Ctrl-b ?

Tmux Cheat Sheet & Quick Reference

tmux weekly

adopt tmux step by step

tmux weekly

week 1 - get started

- 1.1 - start new session
  $ tmux new -s session1

- 1.2 - detach session
  CTRL+b, d
  (keep CTRL+b pressed together and then press d)

- 1.3 - list sessions
  $ tmux ls

- 1.4 attach to session
  $ tmux attach -t session1

week 2 - new windows

- 2.1 - new window
  CTRL+b, c

- 2.2 - move to previous window
  CTRL+b, p

- 2.3 - move to the next window
  CTRL+b, n

- 2.4 - move to window by number
  CTRL+b, <id>

  e.g CTRL+b, 0
      CTRL+b, 1
      ...

week 3 - copy paste

- 3.1 - enter copy mode
  CTRL+b, [

- 3.2 - navigate to position
   option  I - use arrows
   option II - CTRL+n (next line)
               CTRL+p (previous line)
               CTRL+a (beggining of line)
               CTRL+e (end of line)
               CTRL+b (back one character)
               CTRL+f (forward one character)

- 3.3 - start copying
  CTRL+SPACE

  navigate to select the text - same as 3.2

- 3.4 - copy selection
  ALT+w or CTRL+w

- 3.5 - paste (in a different buffer)
 CTRL+b, ]

week 4 - panes

- 4.1 - Split the current pane into two, left and right
  Ctrl+b, %
  
- 4.2 - Split the current pane into two, top and bottom
  Ctrl+b, "
  
- 4.3 - Kill the current pane
  Ctrl+b, x
  
- 4.4 - navigate panes
  Ctrl+b, q      - Briefly display pane indexes
  Ctrl+b, o      - Select the next pane in the current window.
  Ctrl+b, Ctrl+o - Rotate the panes in the current window forwards
  
- 4.5 - Break the current pane out of the window
  Ctrl+b, !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment