If you’re a command-line user, there’s a good chance you’ve come across the best way to handle multiple sessions through a terminal, screen
!
It’s a simple, powerful tool that you can get set up using either
$ sudo apt install screen
or
$ brew install screen
but it’s a little cryptic to use! First, I’d suggest setting it up so you can make it look nicer and be a bit more informative.
In your home directory (~/
) create (or edit) your .screenrc
file. In it, put the following configurations:
startup_message off attrcolor b ".I" term xterm-256color defscrollback 30000 hardstatus alwayslastline hardstatus string '%H %= %t:Screen %n%f %= %Y-%m-%d %c'
This will allow for things like bold text, colors in the terminal, a very long scrollback and an informative status bar across the bottom of the screen.
Now just run
$ screen
in your terminal and you’ll jump into a created screen! But why bother with this when logging in works just as well? Screen has a ton of useful features.
First, let’s go with the basics. Press Ctrl a
and then c
to create and jump to a new screen. If you’re using the screen configuration we set earlier, the only change you’ll see right away is that the Screen 1
at the bottom is being displayed instead of Screen 0
. To move between these screens, type
Ctrl a n
to move to the next screen (it cycles around) or
Ctrl a p
to move to the previous screen.
You can give each of these screens it’s own title to make it easier to notice what screen you might be looking at.
Ctrl a Shift A
will open a prompt “Set window title to:” and will allow you to edit the title text there.
Screen is also able to run in the background. This is useful if you want to keep the same session on a remote machine but log in from different locations.
Ctrl a d
will put the current screen session into a “detached” state. The processes inside that screen session will continue to run (poor-man’s service) and you can see what sessions are running by typing
$ screen -ls
If there is only one detached session, you can easily run
$ screen -r
and immediately reattach to that screen session. If there are multiple detached screens, you’ll have to specify the screen session name or screen id.
There is a screen on: 29529.ttys002.LAPTOP-1083 (Detached)
$ screen -r 29529
Screen has a lot more capabilities than what I covered here, but you can see a list of all of the commands and shortcuts while inside of a screen session if you type
Ctrl a :
and then help
Command key: ^A Literal ^A: a break ^B b clear C colon : copy ^[ [ detach ^D d digraph ^V displays * dumptermcap . fit F flow ^F f focus ^I hardcopy h help ? history { } info i kill ^K k lastmsg ^M m license , lockscreen ^X x log H meta a monitor M next ^@ ^N sp n number N only Q other ^A pow_break B pow_detach D prev ^H ^P p ^? quit ^\ readbuf < redisplay ^L l remove X removebuf = reset Z screen ^C c select ' silence _ split S suspend ^Z z time ^T t title A vbell ^G version v width W windows ^W w wrap ^R r writebuf > xoff ^S s xon ^Q q ^] paste . " windowlist -b select - 0 select 0 1 select 1 2 select 2 3 select 3 4 select 4 5 select 5 6 select 6 7 select 7 8 select 8 9 select 9 ] paste .
Enjoy using screen
!