By Greg Rowe | July 30, 2018
Over the years I’ve spent thousands of hours working with devices
connected via serial ports. Only recently did I learn that the
wonderful GNU screen
can be used as a terminal emulator. screen
has long been popular in the world of systems administration but
rarely do have I seen it used in the context of software development.
Overview
In the Windows world, years ago,
TeraTerm was a popular choice
among knowledgeable developers. It offered a lot more than the
standard Hyper Terminal
application that shipped with MS
Windows. Then again almost any terminal emulator worked better than
Hyper Terminal
. In more recent times putty,
the highly capable ssh
client for Windows gained popularity as a
terminal emulator. Putty
has become so popular that I’ve even seen
developers run it on Linux for the sole reason that they could master
a single tool for both Linux and Windows. If, however, you are
primarily working on Linux then I recommend the use of GNU screen
.
At first screen
can be frustrating. You’ll probably find yourself
asking things like “why isn’t ctrl-a working?” and “how do I exit
this?!“. If you are patient and work through the initial learning
curve you’ll reap rewards.
Launching
Launching screen as a terminal emulator is as easy as directing it to your serial port and telling it which baud rate to use. The example below is for a USB serial adapter.
screen /dev/ttyUSB0 115200
Escape Sequence
If you are an emacs
user or accustomed to bourne shells you’ll very
quickly become frustrated. screen
uses ctrl-a
as an escape sequence
for sending commands to screen
itself rather than the end
terminal. This conflicts with default emacs
keybindings for
‘beginning of line’ which is also the default binding for ‘beginning
of line’ in a bourne shell or readline based application. You can
either get used to this or rebind the key to something else. I opt to
rebind the sequence since emacs
keybindings are permanently burned
into my brain.
To rebind edit ~/.screenrc
with a new sequence. The sequence I chose
is alt-shift-6
as it will not conflict with emacs.
# Make the escape sequence c-s-6
escape ^^^
Basics
I will not attempt to provide a screen
tutorial since there are many tutorials that do a far better job. Instead I will provide the key commands you’ll need to know and let you find your own journey in learning screen
in as much or as little detail as you desire.
Quitting
At some point you’re going to want to exit screen
. To exit use ESCAPE \
where ESCAPE
is whatever sequence you chose for screen
. In my case ctrl-shift-6 \
exits screen
.
Scrolling
Inevitably you will want to look at output that scrolled past the
active window. Using the scroll bars will not work as you expect. This
is because screen
keeps its own scroll back buffer. To scroll back
and (optionally) copy text use ESCAPE esc
. In my case that is
ctrl-shift-6 esc
. You can then use your cursor keys to navigate the
history from the session.
Detaching
One of the best features of screen is that you do not have to quit the
application. You can detach from a session and reattach at a later
time. A common case for this is doing work at your desk and then
continuing your debug session in a lab or at home. To detach press
ESCAPE d
. In my case that is ctrl-shift-6 d
.
Summary
With just a few commands you can be productive with screen
as a
terminal emulator.
- launch -
screen /dev/ttyUSB0 115200
- quit -
ESCAPE \
- detach -
ESCAPE d
- scroll -
ESCAPE esc