Skip to content

Instantly share code, notes, and snippets.

@abalter
Created May 19, 2022 23:04
Show Gist options
  • Save abalter/a8772fcc28308380343fd73bdb2d5027 to your computer and use it in GitHub Desktop.
Save abalter/a8772fcc28308380343fd73bdb2d5027 to your computer and use it in GitHub Desktop.
Screen and Tmux tutorial

Terminal Multiplexers

A terminal multiplexer mainly facilitates three things:

  1. Create persistent terminal sessions that remain intact even when you log out of a machine--as long as the machine itself remains running. For example:
  • SSH into a remote machine
  • Start a screen session
  • Start a process
  • Log out of the machine
  • Log back in later, attach to the screen session, and continue working with that process.
  1. Allow you to work in multiple terminal sessions at the same time without having to launch as individual connections. For example:
  • Open a terminal session and start a multiplexer
  • Create multiple shell sessions in "windows" you can switch between without needing to open additional terminal instance.
  • Split the terminal screen into multiple "panes" which have independent shells.
  • Edit a file in one pane and test running it in the other pane without having to switch between terminal sessions or create a 2nd ssh connection.
  1. Increase productivity by creating work environments (with the above features) which you can detatch from and re-attach to without having to re-create them.

The two most popular terminal multiplexers are screen and tmux. Screen is older and more basic. Tmux is newer and has some snazzier features.

NOTE: As with terminal-based text editors, you can use features of the multiplexer by dropping to a dedicated command line but will almost always use keyboard shortcuts. To access the keyboard shortcuts you preceed them by typing a metacharacter or "prefix." For screen this is CTRL-a. For tmux it is CTRL-b. However you can customize it through a configuration file.

GNU screen

Screen has been around for a long time and I think is installed by default in many Linux distributions. In any case, you can easily install it with the package manager of your choice.

Let's run through some exercises to learn the basics.

Attaching and Detatching

Start a named screen session Open a terminal shell session and type

screen -S test

You will now be in a terminal session and may not see anything different at all. However, type CTRL-a and then t and you will see a little information strip showing that you are in a screen session. You can customize this.

Enter a command Type your favorite shell command like ls or echo hello.

Detacth Type CTRL-a and then d. If your terminal screen looks all messed up it's because some terminals don't clear the screen when you detatch. So just enter the command clear.

List Screen Sessions Type

screen -ls

You should see something like

(base) balter@spectre:~$ screen -ls
There is a screen on:
        7981.test       (05/19/22 15:18:34)     (Detached)
1 Socket in /home/balter/.screen.

Re-attach type

screen -r test

You will now be back in the screen session.

Kill a Screen session The easiest way is to type exit in the screen session. You can also kill it remotely with screen -XS <bame> quit.

There are many ways you can customize your screen session with custom status bars and other niceties.

Screen with SSH

One of the most useful features of screen (or tmux) is that you can log out of an SSH connection and keep a running shell you can get back to.

SSH to a machine For example:

(base) balter@spectre:~$  gcloud beta compute ssh balter-host --zone us-east1-b  --project psjh-eacri-data --tunnel-through-iap
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.13.0-1025-gcp x86_64)

Last login: Thu May 19 19:22:06 2022 from 35.235.240.81
(base) balter@balter-host:~$

Start a screen session screen -S test

Start top Type top

Detatch CTRL-a then d

Logout exit (or whatever)

SSH again gcloud beta compute ssh balter-host --zone us-east1-b --project psjh-eacri-data --tunnel-through-iap

Check Screen Sessions screen -ls

Re-attach screen -r test

Top is still running!!!

Windows

It's pretty common to want to maintain two simultanous SSH connections to a single machine so we can do two things, say monitor a process while doing something else. Or edit a file and run it without having to constantly close the editor.

"Windows" make this possible.

Start a screen session Just like before

Type a command You can use ls so that you will have something on the screen to come back to, or maybe top again.

Start a new window CTRL-a then c

Do something in that window

Switch back to the other window Windows are numbered in the order they are created. So you can type:

CTRL-a then 0 for the first window or CTRL-a then 1 for the 2nd window.

Also, you can type CTRL-a then n for the "next" window and CTRL-a then p for the "previous" window.

Also, you can type CTRL-a then " to get a menu of windows you can navigate through.

I believe there are ways to name windows if you really want to.

Panes

With Panes you can turn a single screen session into something almost like an IDE. You can also mix panes and windows. For instance, you can have two windows, create a 2nd pane in one window, then attach the other window to the 2nd pane.

Create Two Windows As above.

Create a second pane For a vertical divider, type CTRL-a then |. For a horizontal divider, type CTRL-a then %.

Jump to the other pane CTRL-a then TAB

Your cursor will now be in the other pane, but it can't do anything because there is no terminal session there.

Attach the other window Type CTRL-a then t to see which window you are in. The highlighted number is the one you are in.

If the other window is #0, then type

CTRL-a then 0

And window #0 will now be in the 2nd pane.

Switch back and forth between panes CTRL-a then TAB

Do Stuff For example, create a file in one pane

test.py

a = 10
b = 20
print("a*b = ", a*B)

Switch to the other pane

python -i test.py

Etc.

Try doing this in the other order. Create two panes. Jump to the other pane. Then create a new window there CTRL-a then c

Tmux

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