Skip to content

Instantly share code, notes, and snippets.

@Tabea-K
Last active March 29, 2019 08:17
Show Gist options
  • Select an option

  • Save Tabea-K/e69f8077571ac23ebdc675b1aa1b7917 to your computer and use it in GitHub Desktop.

Select an option

Save Tabea-K/e69f8077571ac23ebdc675b1aa1b7917 to your computer and use it in GitHub Desktop.
how to use the screen command

Screen how to

commands that are run from a bash shell, not within the screen session:

to start a "named" screen session, run

screen -mS "my_cool_name"

to start a "named" screen session that also contains the current working directory (or another variable) in the name

screen -mS "my_cool_name ${PWD}"

to reattach to a running screen session use the pid that was assigned to it

screen -r [pid]
# e.g.:
screen -r 93272

if the screen is still attached (e.g. because you attached to it in another terminal window, or there was a connection problem with ssh), you can force reattachment by this command:

screen -rd [pid]

to start a named screen session, run a command within it, and then automatically detach from it, all within one line

screen -dmS "my_cool_name" bash -c "echo Hey I am a screen session"

to list all screen session (if they are named, this name will appear in the list)

screen -ls

There is a screen on:
	93272.my_cool_name	(Detached)
1 Socket in /tmp/screens/S-username.

to directly connect to a specific screen session via ssh (don't need to log in via ssh before running this command)

ssh -p PORTNUMBER -t USER@HOST screen -r SCREEN_NAME

to kill a detached screen session

screen -X -S PID quit

to kill all detached screen sessions

screen -ls | grep Detached | cut -f2 | while read X; do screen -X -S $X quit; done

To start several windows within an already existing screen session and immediately run a command within them

screen -S vcf -X screen_session_name -t tab1 echo "hello world"

commands and keyboard combinations that are run from within a screen session

commands

keyboard combinations

To detach from a running screen session:

first press ctrl+a, followed by d.

To switch to the next running screen session:

first press ctrl+a, followed by n.

To create a new, additional screen session: first press ctril+a, followed by c.

To terminate a running screen session (and also terminating all processes that run within this session!):

press ctrl+d.

Change the title of the screen window

The screen window title is not the same as the screen session title! It is displayed in the bottom of the screen session, when you are using the same .screenrc as I am.

echo -e '\033k'Running in dir: $PWD'\033\\' # this sets the current working directory as the title.

to set the title of a screen window within a session that already runs

screen -t "name"

or

To change the title of a screen window (not the screen session name), press ctrl+a, then type

:title TITLE

Change the title of the screen session

:sessionname TITLE

Adapting the file ~/.screenrc to customize screen

I adapted my ~/.screenrc file so that it always displays the name of the screen session in the bottom. My ~/.screenrc file looks like this:

# Enable mouse scrolling and scroll bar history scrolling for screen
termcapinfo xterm* ti@:te@

# Always display title and number of screen in the bottom status line
hardstatus alwayslastline '%{= kg}[ %{G}%H %{g}][%= %{= kB}%?%-Lw%?%{+b r}(%{G}%n*%f %t%?(%u)%?%{r})%{-b B}%?%+Lw%?%?%= %{g}%]'

# set scrollback buffer higher
defscrollback 10000

altscreen on

Attach each detached screen session in a loop

Beware: This is a rather hacky way to get the PID of the screen session!

for s in `screen -ls | grep Detached | egrep -o "[0-9][0-9]+\." | sed "s/\.//"`
do
screen -r $s
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment