Skip to content

Instantly share code, notes, and snippets.

@jctosta
Last active April 18, 2024 18:19
Show Gist options
  • Save jctosta/af918e1618682638aa82 to your computer and use it in GitHub Desktop.
Save jctosta/af918e1618682638aa82 to your computer and use it in GitHub Desktop.
Screen Cheatsheet

Screen Quick Reference

Basic

Description Command
Start a new session with session name screen -S <session_name>
List running sessions / screens screen -ls
Attach to a running session screen -x
Attach to a running session with name screen -r <session_name>
Detach a running session screen -d <session_name>
Kill a running session screen -X -S [session # you want to kill] kill
Accessing a screen that is already attached screen -r -d [session name]

Escape Key

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

Getting Out

Description Command
detach Ctrl-a d
detach and logout (quick exit) Ctrl-a D D
exit screen Ctrl-a : quit or exit all of the programs in screen.
force-exit screen Ctrl-a C-\ (not recommended)

Help

Description Command
See help Ctrl-a ? (Lists keybindings)

Window Management

Description Command
Create new window Ctrl-a c
Change to last-visited active window Ctrl-a Ctrl-a (commonly used to flip-flop between two windows)
Change to window by number Ctrl-a <number> (only for windows 0 to 9)
Change to window by number or name Ctrl-a ' <number or title>
Change to next window in list Ctrl-a n or Ctrl-a <space>
Change to previous window in list Ctrl-a p or Ctrl-a <backspace>
See window list Ctrl-a " (allows you to select a window to change to)
Show window bar Ctrl-a w (if you don't have window bar)
Kill current window Ctrl-a k (not recommended)
Kill all windows Ctrl-a \ (not recommended)
Rename current window Ctrl-a A

Split screen

Description Command
Split display horizontally Ctrl-a S
Split display vertically `Ctrl-a
Jump to next display region Ctrl-a tab
Remove current region Ctrl-a X
Remove all regions but the current one Ctrl-a Q

Misc

Description Command
Redraw window Ctrl-a C-l
Enter copy mode Ctrl-a [ or Ctrl-a <esc> (also used for viewing scrollback buffer)
Paste Ctrl-a ]
Monitor window for activity Ctrl-a M
Monitor window for silence Ctrl-a _
Enter digraph (for producing non-ASCII characters) Ctrl-a Ctrl-v
Lock (password protect) display Ctrl-a x
Enter screen command Ctrl-a :
Enable logging in the screen session Ctrl-a H

Scrolling

Description Command
Enter scrolling mode Ctrl-a esc
Scroll Up Ctrl-u
Scroll Down Ctrl-d
Exit scrolling mode esc esc

This section is a contribution from: @mickpbarry

@aarobender
Copy link

@zigmoo thank you sir

@wuarmin
Copy link

wuarmin commented Nov 26, 2020

great!

@guterz
Copy link

guterz commented Dec 1, 2020

Can you add how to kill a session?
screen -X -S [session # you want to kill] kill

@jctosta
Copy link
Author

jctosta commented Dec 1, 2020

Can you add how to kill a session?
screen -X -S [session # you want to kill] kill

Done

@mickpbarry
Copy link

Scrolling:

Ctrl-a esc - Enter scrolling mode
Ctrl-u - scroll up
Ctrl-d - scroll down
esc esc - Exit scrolling mode

@lalitjg
Copy link

lalitjg commented Aug 2, 2021

Can you add the following command to access a screen that is already attached?
screen -r -d [session name]

@jctosta
Copy link
Author

jctosta commented Aug 2, 2021

Can you add the following command to access a screen that is already attached?
screen -r -d [session name]

Done

@jctosta
Copy link
Author

jctosta commented Aug 2, 2021

Scrolling:

Ctrl-a esc - Enter scrolling mode
Ctrl-u - scroll up
Ctrl-d - scroll down
esc esc - Exit scrolling mode

Ty man, cheatsheet updated!!!

@Vaibhav-Nf
Copy link

how to broadcast command to all splits

@milahu
Copy link

milahu commented Apr 3, 2022

Send commands or input to a detached screen session

screenName=$(mktemp -u screen-session-XXXXXXXX)
screen -S "$screenName" -d -m # create a new screen session. dont attach
screenCommand="echo \"hello world\""
screen -S "$screenName" -X stuff "$screenCommand^M" # ^M = enter
sleep 0.1
screen -S "$screenName" -X stuff "read^M" # start process
sleep 0.1
screen -S "$screenName" -X stuff "^C" # ^C = Ctrl+C = kill process
screen -r "$screenName" # attach
# detach with Ctrl-a d
screen -S "$screenName" -X quit # kill the detached screen session

capture all *visible* output of a process (text screenshot)

screenName=$(mktemp -u screen-session-XXXXXXXX)
screen -S "$screenName" -d -m # create a new screen session. dont attach
captureCommand="cd $(mktemp -d); npm init -y; npm install cowsay" # example
screenLock=$(mktemp /tmp/screen-lock-XXXXXXXX)
screenCommand="$captureCommand; rm $screenLock;"
echo "start captureCommand"
screen -S "$screenName" -X stuff "$screenCommand^M" # ^M = enter
hardcopyFile=$(mktemp /tmp/hardcopy-XXXXXXXX)
enableWatcher=true
#enableWatcher=false
if $enableWatcher; then
  echo "start watcher"
  (
    # watcher: show live output while waiting
    while true
    #for watcherStep in $(seq 0 100) # debug
    do
      sleep 2
      #echo watcher step "$watcherStep"
      screen -S "$screenName" -X hardcopy -h "$hardcopyFile" # take screenshot. -h = include history
      #cat "$hardcopyFile"
      tail "$hardcopyFile"
    done
  ) &
  watcherPid=$!
  echo "watcherPid = $watcherPid"
fi
echo "wait for captureCommand ..."
while true
#for waiterStep in $(seq 0 100) # debug
do
  sleep 1
  #echo waiter step "$waiterStep"
  [ -e "$screenLock" ] || break
  done
echo "done captureCommand"
if $enableWatcher; then
  echo "stop watcher"
  kill $watcherPid
fi
screen -S "$screenName" -X hardcopy -h "$hardcopyFile" # take screenshot. -h = include history
echo "done hardcopy $hardcopyFile"

@jollymike
Copy link

jollymike commented Apr 25, 2022

Rename Session:

ctrl+a :sessionname <desired session name>

*be sure to include the ':' before typing 'sessionname ...'

@alexb-git
Copy link

alexb-git commented May 28, 2023

changed order of screens:
Open :windowlist (Ctrl-a ") move windows up , (comma) and down with . (dot)

0 user <- selected | pressed '.' | 0 log              |  pressed ',' |  0 user <- selected
1 log              |             | 1 user <- selected |              |  1 log 

@sanyamsmulay
Copy link

Hey !

  • | exit screen | Ctrl-a : quit or exit all of the programs in screen.|
    +| exit screen | Ctrl-a :quit to quit or exit all of the programs in screen.|

I had not realised that the quit needs to be typed.
Hopefully highlighting the quit would be helpful.

Here's the diff:
https://gist.github.com/sanyamsmulay/410cfe2123a4ff54acd7d22b65074ad0/revisions?source=1#diff-c1482fcf0d55b3ecb91770624a12c26c5e95d40bfa732406d2abb03b6a3e0e9dL24

@MohammedRakib
Copy link

Can you please add kill all screens:

killall screen

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