Using GNU Screen (when there’s no tmux)

TLDR: using screen when tmux is not available. This isn’t a tmux vs screen post but rather how to use the tools that you have to get the job done.

It’s hard to imagine a day spent on a shell without the use of a terminal multiplexer. My favourite tool for the job is tmux, which comes handy when I’m working on or from an OpenBSD machine. tmux is installed by default on OpenBSD for quite a few releases now (circa 2009), you can see the commit log here.

It is easy to get used to something good ūüėČ However, IT being IT, this wouldn’t be so easy. One reason that I see, is that a lot of servers out there running some sort of Unix-like OS (mainly, Linux) do not have tmux installed. Now the interesting side of this is that I often find the GNU screen installed.

Let’s take SLES 12 as an example. Here’s how to see the version of screen if have on your system:

headless1:~ # rpm -qa|egrep screen

The default configuration on SLES is pretty rough, and if you fire up¬†screen¬†you really can’t tell whether or not you are running it.

screen initial welcome message

screen initial welcome messageScreen Shot 2015-11-27 at 9.50.45 AM.png

screen doesn’t doesn’t give you more information by default (at least not on SLES 12). tmux¬†on the other hand already provides some information.

Screen Shot 2015-11-27 at 9.57.45 AM.png tmux on OpenBSD 5.7

Wouldn’t be great to make¬†screen¬†look a little like¬†tmux ? I think it would, let’s give a shot. screen¬†comes with a great manual (man screen), from which you can take a lot of information to tweak your .screenrc¬†¬†file, this can also be a daunting process because of the plethora of information there, so here’s an example that works for me:

# turn off the initial welcome message
startup_message off

# scroll back buffer for new windows. Default is 100 lines.
defscrollback 10000

# changes the default ctrl-a to ctrl-b. This will mimic tmux
escape ^Bb

# fixes issues for some terminals (i.e.: xterm)
termcapinfo xterm* ti@:te@

# enables mouse selection on split mode
#mousetrack on

# reload. meta-b meta-r
bind ^r source $HOME/.screenrc

# status bar will resemble tmux.
hardstatus alwayslastline
hardstatus string '%{= gk}%?%-w%?%n %t*%+w %= "%H"  %c %d-%M-%Y'

At this point, we have the information about what windows are running (a la tmux). This is how it looks:

Screen Shot 2015-11-27 at 11.50.05 AM.png

I also like to work on a split view mode, especially when troubleshooting something. You can keep a log in one side and work on the other one.

Screen Shot 2015-11-27 at 11.58.38 AM.png

To wrap up, here are a few shortcuts that I use the most. Remember, based on the above configuration, ctrl-a is now ctrl-b (abbreviated as C-b below). Please note that most of these commands can be invoked with more than one key binding (i.e.: to a create a new window, one can use “C-b c” or “C-b C-c”. Refer to the man page for more details)

Key Bindings

C-b c Create a new window with a shell and switch to that window.
C-b tab Switch the input focus to the next region.
C-b A Allow the user to enter a name for the current window.
C-b d Detach screen from this terminal.
C-b H Begins/ends logging of the current window to the file “screenlog.n”.
C-b M Toggles monitoring of the current window.
C-b n Switch to the next window.
C-b x Lock this terminal.
C-b X Kill the current region.
C-b ? Show key bindings.
C-b C-\ Kill all windows and terminate screen.
C-b : Enter command line mode.
C-a ] Enter copy/scrollback mode.
C-a ] Write the contents of the paste buffer to the stdin queue of the current window.
C-a _ Start/stop monitoring the current window for inactivity.
C-a * Show a listing of all currently attached displays.

Basic commands (excerpts from the man page)

screen start screen
screen -d -r Reattach a session and if necessary detach it first.
screen -ls does not start screen, but prints a list of pid.tty.host strings identifying your screen sessions. Sessions marked `detached’ can be resumed with “screen -r”. Those marked `attached’ are running and have a controlling terminal.
screen -L tells screen to turn on automatic output logging for the windows.
screen -r sessionowner/[pid.tty.host] resumes a detached screen session. No other options (except combinations with -d/-D) may be specified, though an optional prefix of [pid.]tty.host may be needed to distinguish between multiple detached screen sessions. The second form is used to connect to another user’s screen session which runs in multiuser mode. This indicates that screen should look for sessions in another user’s directory. This requires setuid-root.

Extra reading:

Arch Linux GNU screen: Lot’s of different tweaks and configurations explained here.

Screen quick reference guide.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s