Even Better GNU Screen

Nov 13, 2009

GNU Screen is one of my favorite applications because of its simplicity and usefulness. And, as a fairly new user, I am constantly discovering features and awesome ways to interface it with my systems.

Following are some tips and links I’ve picked up that have vastly improved my screen experience, including how to save region placement, how to use zsh to save history across multiple shell sessions, and how to use ssh-agent with screen to ease remote access.

But before we get into the meat, let’s talk vocabulary (taken from the GNU docs):

A screen session is opened when you launch GNU Screen by typing screen in a (pseudo)terminal or via a script.

A window is a shell session opened inside of screen when you begin a new screen session or manually open a window with the ^a c command (that’s Control-a [pause] c).

A region is a portion of one window that displays the contents of another window. IE, a horizontal or vertical split.

Okay, with vocab out of the way, let’s get started!

Saving Region Placement

I was lurking in #screen on Freenode one day when I picked up this handy tip. To save the placement of your vertical and horizontal window splits, just use nested screen sessions.

First, log in to a shell (via ssh, a terminal window, etc.), then open a screen session using the screen command.

Inside of screen, open three or four windows (with ^a c) — one for each of the windows you want to save the placement of. I usually open windows for irssi, htop and iptraf, for example.

Now, detach the screen with ^a d. You’re back at the shell. Verify that screen is still running with the screen -ls command, and save the ID number of the session (which appears before .[terminal].[hostname].

For example:

andrew@levin:~$ screen -ls

There are screens on:

6718.pts-0.levin (02/08/2009 08:40:36 AM) (Multi, attached)

Here, the ID of the session is 6718.

To begin nesting, open a new screen session by typing screen again. Then, use the ID of the first session to reconnect to that session from within the second session (you can verify that two sessions are now running with the screen -ls command):

andrew@levin:~$ screen -ls

There are screens on:

31333.pts-0.levin (02/08/2009 08:40:36 AM) (Multi, attached)

6718.pts-0.levin (02/03/2009 09:43:53 PM) (Multi, detached)

2 Sockets in /var/run/screen/S-andrew.

andrew@levin:~$ <strong>screen -r 6718</strong>

And there you go — you have just nested one screen session within another.

Now, you can setup your vertical and horizontal window splits, AKA regions, in the first screen session and the settings will persist in the second session until the computer shuts down, the session dies, or you readjust the splits.

Caveat: Each nested session incurs an additional a in your command sequence.

So, demonstrating the previous example, to setup your persistent splits in the nested session, you would hit: ^a a | for a vertical split or ^a a s for a horizontal split.

Or, to tab between regions in the nested session, hit: ^a a tab (as opposed to switching in the first layer, which would be ^a tab).

Finally, to swtch windows in the nested layer, hit ^a a [window number] (IE, ^a a 1) if you know the identifier for the window, or ^a a " to list all opened windows.

That’s it!

Zsh Screen = Common Shell History

How many times have I searched my shell history for a recent command only to remember that I executed it in another screen session or window?

The amazing zsh, an excellent replacement for bash, eases command reuse in screen by allowing you to share command history among all shell sessions.

The .zshrc options to do this are setopt SHARE_HISTORY and setopt APPEND_HISTORY.

For more info on this and other history-related options for your .zshrc, check out this primer and some examples at dotfiles.org. Try urukrama’s .zshrc file to start things off.

Ssh-agent Screen = Easy Remote Login

The last tip is to setup screen to work with ssh-agent, which will let you more easily connect to systems onto which you’ve installed a (password encrypted) key. MBrisby will take us out on this one: GNU screen w/ ssh-agent.

More Screen Love

For an intro to screen, see Jonathon McPherson’s excellent how-to.

For more great screen tricks (including some good ones I use every day), check out Phil Sung’s 2008 blog post.

Happy screening!

GNU Screen is one of my favorite applications because of its simplicity and usefulness. And, as a fairly new user, I am constantly discovering features and awesome ways to interface it with my systems.

Following are some tips and links I’ve picked up that have vastly improved my screen experience, including how to save region placement, how to use zsh to save history across multiple shell sessions, and how to use ssh-agent with screen to ease remote access.

But before we get into the meat, let’s talk vocabulary (taken from the GNU docs):

A screen session is opened when you launch GNU Screen by typing screen in a (pseudo)terminal or via a script.

A window is a shell session opened inside of screen when you begin a new screen session or manually open a window with the ^a c command (that’s Control-a [pause] c).

A region is a portion of one window that displays the contents of another window. IE, a horizontal or vertical split.

Okay, with vocab out of the way, let’s get started!

Saving Region Placement

I was lurking in #screen on Freenode one day when I picked up this handy tip. To save the placement of your vertical and horizontal window splits, just use nested screen sessions.

First, log in to a shell (via ssh, a terminal window, etc.), then open a screen session using the screen command.

Inside of screen, open three or four windows (with ^a c) — one for each of the windows you want to save the placement of. I usually open windows for irssi, htop and iptraf, for example.

Now, detach the screen with ^a d. You’re back at the shell. Verify that screen is still running with the screen -ls command, and save the ID number of the session (which appears before .[terminal].[hostname].

For example:

andrew@levin:~$ screen -ls

There are screens on:

6718.pts-0.levin (02/08/2009 08:40:36 AM) (Multi, attached)

Here, the ID of the session is 6718.

To begin nesting, open a new screen session by typing screen again. Then, use the ID of the first session to reconnect to that session from within the second session (you can verify that two sessions are now running with the screen -ls command):

andrew@levin:~$ screen -ls

There are screens on:

31333.pts-0.levin (02/08/2009 08:40:36 AM) (Multi, attached)

6718.pts-0.levin (02/03/2009 09:43:53 PM) (Multi, detached)

2 Sockets in /var/run/screen/S-andrew.

andrew@levin:~$ <strong>screen -r 6718</strong>

And there you go — you have just nested one screen session within another.

Now, you can setup your vertical and horizontal window splits, AKA regions, in the first screen session and the settings will persist in the second session until the computer shuts down, the session dies, or you readjust the splits.

Caveat: Each nested session incurs an additional a in your command sequence.

So, demonstrating the previous example, to setup your persistent splits in the nested session, you would hit: ^a a | for a vertical split or ^a a s for a horizontal split.

Or, to tab between regions in the nested session, hit: ^a a tab (as opposed to switching in the first layer, which would be ^a tab).

Finally, to swtch windows in the nested layer, hit ^a a [window number] (IE, ^a a 1) if you know the identifier for the window, or ^a a " to list all opened windows.

That’s it!

Zsh Screen = Common Shell History

How many times have I searched my shell history for a recent command only to remember that I executed it in another screen session or window?

The amazing zsh, an excellent replacement for bash, eases command reuse in screen by allowing you to share command history among all shell sessions.

The .zshrc options to do this are setopt SHARE_HISTORY and setopt APPEND_HISTORY.

For more info on this and other history-related options for your .zshrc, check out this primer and some examples at dotfiles.org. Try urukrama’s .zshrc file to start things off.

Ssh-agent Screen = Easy Remote Login

The last tip is to setup screen to work with ssh-agent, which will let you more easily connect to systems onto which you’ve installed a (password encrypted) key. MBrisby will take us out on this one: GNU screen w/ ssh-agent.

More Screen Love

For an intro to screen, see Jonathon McPherson’s excellent how-to.

For more great screen tricks (including some good ones I use every day), check out Phil Sung’s 2008 blog post.

Happy screening!

Follow my posts