The Fugue Counterpoint by Hans Fugal


Pimping Screen

GNU Screen is a fantastic tool. It gives you detachable terminal sessions with multiple screens. Most Linux types are familiar with that basic functionality, for which you need to know a very few things: screen starts a screen, C-a c to create a new screen, C-a n and C-a p to navigate, C-a d to detach, and screen -r to reattach.

But when I'm really in the zone, C-a n/p is too many keystrokes. And I'd like to have a nifty "tab bar", and I'd like to not get lost. Well, I just pimped my screen and now I'll walk you through it.

First, let's get some status information to keep us from getting lost. Add this to ~/.screenrc:

shelltitle "$ |bash"
hardstatus alwayslastline "%{= dg}%Lw%=%H"

The first line assumes your prompt ends in a '$ ' like mine does. Now, to complete the effect, put this in ~/.bashrc:

export PROMPT_COMMAND='echo -ne "\ek\e\\"'

There, a nice "tab bar" that tells you your tabs and the host on which this screen session is running. And it's green. What else could you ask for?

Now, let's set up some awesome keybindings. We'll use C-left and C-right:

# ctrl-left/right to switch windows
bindkey "^[[5D" prev # osx
bindkey "^[[5C" next
bindkey "^[[1;5D" prev # linux
bindkey "^[[1;5C" next
# ctrl-T to start a new 'tab' (c-a c)
bindkey "^T" screen

What would you expect to pay for a screen that can do all this? $50? $100? $19.95? But wait, there's more!

Now wouldn't it be nice if you knew which host you were ssh'd into (and your editor or some other program is running so there's no prompt to tell you)? Sure, and I'll show you how. I'll do better than that though. I'll also show you something else you didn't know you wanted:

# in ~/.ssh/config
Host *
PermitLocalCommand yes
LocalCommand /usr/bin/printf '\ek%h\e\\'
# in ~/.bashrc
alias ssh='screen -- ssh'

The alias causes us to start a new screen for any ssh connection, which keeps your local screen pristine. That's kind of neat.

Update: If you use the LocalCommand thing in ssh, it can break rsync with a cryptic message about protocol versions. The solution is to do this in ~/.bashrc:
export RSYNC_CONNECT_PROG='ssh -o PermitLocalOption=no '

Comments (3) Trackbacks (1)
  1. Great tips for screen – I’ve been putting off going through the man page for years to try and figure this stuff out!


Leave a comment