Skip to content

Instantly share code, notes, and snippets.

@aviafelix
Created January 18, 2018 01:41
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 aviafelix/7a6a9cb31ec6ce181ad862e28e60f102 to your computer and use it in GitHub Desktop.
Save aviafelix/7a6a9cb31ec6ce181ad862e28e60f102 to your computer and use it in GitHub Desktop.

Screen Cheatsheet and Shortcuts

Compilation of:


Console Commands

Start a new session with name:

screen -S <name>

List of running screen sessions:

screen -ls
# or
screen -list

Attach to the running screen session (with name):

screen -x [session_name]
# or:
screen -r <session_name>

The “ultimate attach”:

screen -dRR

(Attaches to a screen session. If the session is attached elsewhere, detaches that other display. If no session exists, creates one. If multiple sessions exist, uses the first one.)

Default Key Bindings

Escape key

All screen commands are prefixed by an escape key, by default Ctrl+a (that's sometimes written ^a). To send a literal Ctrl+a to the programs in screen, use Ctrl+a a. This is useful when working with screen within screen. For example Ctrl+a a n will move screen to a new window on the screen within screen.

Common Commands

Getting Out

Command Description
Ctrl+a d Detach from the current screen session, and leave it running. Use screen -r to resume
Ctrl+a D D Detach and logout
exit Getting out from the screen session

Window Management

Command Description
Ctrl+a c Create a new window (with shell)
Ctrl+a Ctrl+a Toggle between current and previous region
Ctrl+a <number> Change to window by number: opens window <number>
Ctrl+a 0 Change to window by number: opens window 0
Ctrl+a ' <number or title> Change to window by number or name
Ctrl+a n Change to next window in list
Ctrl+a Space Change to next window in list
Ctrl+a p Change to previous window in list
Ctrl+a Backspace Change to previous window in list
Ctrl+a " See window list
Ctrl+a w Show window bar
Ctrl+a A Rename the current window
Ctrl+a a Sends Ctrl+a to the current window

Split Screen

Command Description
Ctrl+a S Split current region horizontally into two regions
Ctrl+a | Split current region vertically into two regions
Ctrl+a V Split current region vertically into two regions (for the vanilla vertical screen patch)
Ctrl+a Tab Switch the input focus to the next region
Ctrl+a X Close the current region
Ctrl+a Q Close all regions but the current one

Clipboard and Navigation

Command Description
Ctrl+a [ Enter Copy Mode (freely navigate buffer; use enter to select a range of text)
Ctrl+a Esc Enter Copy Mode (freely navigate buffer; use enter to select a range of text)
Space Toggle selection to copy
Ctrl+a ] Paste text

Help

Command Description
Ctrl+a ? See help: displays commands and their defaults

Miscellaneous

Command Description
Ctrl+a Ctrl+l Redraw window
Ctrl+a M Monitor window for activity
Ctrl+a _ Monitor window for silence
Ctrl+a Ctrl+v Enter digraph (for producing non-ASCII characters)
Ctrl+a x Lock (password protect) session
Ctrl+a : Enter screen command
Ctrl+a H Enable logging in the screen session

Scrollback-buffer

Command Description
Ctrl+u Half page up
Ctrl+d Half page down
Ctrl+b Back
Ctrl+f Forward
h, j, k, l Cursor left/down/up/right

Command Prompt Commands

Command Description
Ctrl+a : Enter to the command prompt of screen
Ctrl+a :quit Closes all windows and closes screen session
Ctrl+a :source ~/.screenrc Reloads screenrc configuration file (can alternatively use /etc/screenrc)

Nested Screen Sessions

It is possible to get stuck in a nested screen session. A common scenario: you start an SSH session from within a screen session. Within the SSH session, you start screen. By default, the outer screen session that was launched first responds to Ctrl+a commands. To send a command to the inner screen session, use Ctrl+a a, followed by your command. For example:

Command Description
  • Ctrl+a a d | Detaches the inner screen session.
  • Ctrl+a a K | Kills the inner screen session.

Scripting

Send a command to a named session:

screen -S <name> -X <command>

Create a new window and run ping example.com:

screen -S <name> -X screen ping example.com

Stuff characters into the input buffer using bash to expand a newline character:

screen -S <name> [-p <page>] -X stuff $'quit\r'

A full example:

# run bash within screen
screen -AmdS bash_shell bash
# run top within that bash session
screen -S bash_shell -p 0 -X stuff $'top\r'
 
# ... some time later
 
# stuff 'q' to tell top to quit
screen -S bash_shell -X stuff 'q'
# stuff 'exit\n' to exit bash session
screen -S bash_shell -X stuff $'exit\r'

tmux Cheatsheet and Shortcuts

Compilation of:


Console Commands

Start a new session:

tmux

Start a new session with name:

tmux new -s <name>

Attach to the running tmux session:

tmux a # or at, or attach

Attach to the running tmux session with name (<name> is # for non-named session):

tmux a -t <name>

Attach to the running tmux session if exists else start new one:

tmux attach || tmux new

List of running tmux sessions:

tmux ls

Switch to the session with name:

tmux switch -t <session_name>

Kill session:

tmux kill-session -t <name>

Kill all the tmux sessions (not recommended):

tmux ls | grep : | cut -d. -f1 | awk '{print substr($1, 0, length($1)-1)}' | xargs kill

Better way to kill all the tmux sessions:

tmux ls | awk -F: '{print $1}' | xargs -i tmux kill-session -t {}

Default Key Bindings

Sessions

Command Description
Ctrl+b :new<cr> New session
Ctrl+b s List sessions
Ctrl+b $ Name session

Windows (tabs)

Command Description
Ctrl+b c Create new window
Ctrl+b , Name window
Ctrl+b w List all windows / window numbers: select from interactive list of windows
Ctrl+b <window-number> Switch to the window with number <window-number>
Ctrl+b n Next window
Ctrl+b p Previous window
Ctrl+b l Last window: move to the previously selected window
Ctrl+b f Find window: search for the window name
Ctrl+b & Close (kill) window
Ctrl+b . Move window — prompted for a new number
Ctrl+b :movew<cr> Move window to the next unused number

Panes (splits)

Command Description
Ctrl+b % Vertical split
Ctrl+b " Horizontal split
Ctrl+b o Swap Switch to the next pane
Ctrl+b Ctrl+o Swap panes
Ctrl+b q Show pane numbers; when the numbers show up type the key to goto that pane
Ctrl+b x Close (kill) pane
Ctrl+b + Break pane into window (e.g. to select text by mouse to copy)
Ctrl+b - Restore pane from window
Ctrl+b <Space> Toggle between layouts
Ctrl+b z Toggle pane zoom
Ctrl+b { Move the current pane left
Ctrl+b } Move the current pane right
Ctrl+b <Arrow-Key> Toggle between panes
Ctrl+b <PgUp> Enter the "copy mode", and then scroll with <PgUp> and <PdDown>; <q> quit from "copy mode"
Ctrl+b [ Enter the "copy mode"; quit: <q> (vi mode), <Esc> (emacs mode)
Ctrl+b Ctrl+<Arrow-Key> Change panel size by 1 cell
Ctrl+b Alt+<Arrow-Key> Change panel size by 5 cells
Ctrl+b Alt+<number> Panes aligments

Window/pane Surgery

Command Description
Ctrl+b :joinp -s :2<cr> Move window 2 into a new pane in the current window
Ctrl+b :joinp -t :1<cr> Move the current pane into a new pane in window 1
Ctrl+b :setw synchronize-panes `[on off]`
Ctrl+b : resize-pane -D [<number>] Resizes the current pane down (by <number> cells)
Ctrl+b : resize-pane -U [<number>] Resizes the current pane upward (by <number> cells)
Ctrl+b : resize-pane -L [<number>] Resizes the current pane left (by <number> cells)
Ctrl+b : resize-pane -R [<number>] Resizes the current pane right (by <number> cells)
Ctrl+b : resize-pane -t <pane-id-number> -L <number> Resizes the pane with the id of <pane-id-number> left by <number> cells

Miscellaneous

Command Description
Ctrl+b d Detach
Ctrl+b t Big clock
Ctrl+b ? List shortcuts
Ctrl+b : Prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment