Short Tip: GNU Screen with proper scroll, session management support

shell.png
I often use GNU Screen – in fact, I can hardly imagine anyone administering Linux servers without it. However, I always found the scroll support in GNU Screen awkward: Ctrl+A+Esc (although this is, strictly speaking, the copy mode).

This can be fixed by adding this line to .screenrc:

termcapinfo xterm* ti@:te@ 

The only problem is that, while scrolling works perfect in normal terminal sessions, scrolling in vim in screenrc in KDE 4’s Konsole still does not work properly.

And since I am already talking about GNU Screen, these lines add a white control bar showing the currently open sessions, their names and make it possible to switch between them with F5 and F6:

caption always # activates window caption
caption string '%{= wk}[ %{k}%H %{k}][%= %{= wk}%?%-Lw%?%{r}(%{r}%n*%f%t%?(%u)%?%{r})%{k}%?%+Lw%?%?%= %{k}][%{b} %d/%m %{k}%c %{k}]' # good looking window bar

bindkey -k k5 prev # F5 for previous window
bindkey -k k6 next # F6 for next window

You can rename sessions with Ctrl+a+A.

As usual I appreciate comments and further tips – but please don’t paste anything with more than 5 or 6 lines of code ;)

About these ads

14 thoughts on “Short Tip: GNU Screen with proper scroll, session management support

  1. You know what the funniest part is, its not actually Ctrl-a [esc].
    The official key is the ‘[‘.

    The reason why esc works is because TTY-s send the esc key escaped over the line and the escape sequence starts with a ‘[' :)

    Note that if you want to paste that the Ctrl-a-] combo then makes a bit more sense ;)

  2. When you switch between sessions, and then try to scroll back, you get a mix of the activity in the different sessions in the scroll-back. Any way to have a scroll-back buffer per session?

  3. I like the F5/F6 thanks :)
    I agree with Roger about the mix in sessions.
    Also, here is my version of the bottom line, blue and red, including the system load :
    backtick 2 0 0 whoami
    caption always “%{.bW}%-w%{.rW}%n %t%{-}%+w %= %{..W}[%l] %{..G}%2`@%H %{..Y} %D %d/%m/%Y %c “

  4. Scrolling in Vim under screen with or without the termcap line in .screenrc works correctly here and I haven’t changed anything in the scrolling code since 4.1. Visual selection with the mouse behaves differently under screen in xterm vs Konsole though. This is tested under Ubuntu 8.10 Beta. Might be worth checking behavior with an empty .screenrc and see if that works. Unless it looks like a Fedora-specific problem then please file a bug report.

  5. Scrolling in Vim under screen also works for me in konsole. I use opensuse 11.0/11.1Beta

  6. Put this in your bash shell environment and your windows will name themselves (unless you rename them after)

    [[ "${TERM}" == "screen" ]] && echo -n -e ‘\ek’${HOSTNAME%%.*}’\e\’

    hopefully the back-slashes won’t get eaten… it’s

    ‘BACKSLASHek’DOLLARcurlyHOSTNAME%%.*curly’BACKSLASHeBACKSLASH’

  7. Thomas Zander, interesting to know :)
    Robert, Not Plubmer Joe: that surprised me, I will try to reproduce it here in virtual machines!
    Sam, the problem is that now I’m not sure what you exactly meant. If you want to post sourcecode here, read this short howto.

  8. @Thomas Zander, you’re a bit off there. C-a [ and C-a C-[ are both quite official bindings for copy mode in screen. You’ll see this in the manual (which in fact also lists C-a esc, even though that’s identical to C-a C-[). Also TTYs don’t send a special escape sequence beginning with “[” to represent escape; escape is always represented by itself (escape sequences actually always start with the escape character, which can also be represented as ^[; they also often, but not always, follow that with a [. For instance, the sequence to set boldface text is “^[[1m”, where ^[ is the escape character.

  9. My post worked fine…

    [[ "${TERM}" == "screen" ]] && echo -n -e ‘\ek’${HOSTNAME%%.*}’\e\’

    If that’s in your bash environment, your screen windows will auto-name themselves after whatever host you’re connected to. So if you, like me, start screen on your desktop and within screen connect to lots of hosts, your shell environment on those hosts will name the screen for you so that you don’t have to (hence $HOSTNAME).

    If you still don’t get it, start screen and paste that and then hit ctrl-a-”

    Of course you could use whatever string you want and even have a shell-alias for setting the screen name instead of having to interact with screen directly… avoiding a context switch in the brain if you will.

  10. Thanks for the tip. If anybody is using this tip with moc and screen, you will notice that moc sets the title. The title set by moc takes a lot of space.

    If you are using the 2.5.0-alpha2 version you can add the option ‘SetScreenTitle = no’ to your config file. Then you can set your own title without moc doing it.

Comments are closed.