Site Tools


Interacting with screen

Basics

This set of commands is mostly about customizing screen's interface, in terms of keybindings. The most important command is probably escape, which lets you change the default screen prefix escape character. emacs users, in particular, will want to use a different prefix, since C-a is “beginning of line” in emacs.

The bind and bindkey commands allow you to add or redefine screen keybindings. Both can associate arbitrary screen commands with specific keypresses. bind defines keys in screen's main command class, so the command “bind K kill” would cause the key sequence C-a K to trigger the kill command. bindkey is more general; it can bind commands to any keypress, not just ones preceded by C-a. bindkey can also modify the keypresses used in copy mode.

You can use C-a ? to see what the current set of keybindings is.

Extras

bindkey Tables

Technically, what bindkey does is modify one of screen's three input translation tables. Normally, when you press a key, screen checks the user translation table to see if they key is there; if it is, then the bound action is taken. bindkey changes the user translation table by default, and most arbitrary keybindings should go there. If the user translation table doesn't have an entry for the key pressed, screen then checks the default translation table; if nothing is there, the keypress gets sent to the window. The default translation table is used for keybindings that affect screen's terminal emulation, like the particular escape sequence to send for a function key.

The third input translation table is for copy mode. You can use it to change the keys used in that mode. This works a little differently than the other modes, because there aren't commands for the things you can do in copy mode. You can, of course, bind any of screen's normal commands, but if you want to trigger any copy-mode-specific commands, you have to use stuff and stuff the key sequence for the action you want to take. You can use this functionality to remap copy mode key bindings, but that's better done with the markkeys command; bindkey is more useful when you want to chain several actions together.

bindkey updates the user table by default, and the default and copy mode tables with the -d and -m options, respectively. If you call bindkey without giving it a keybinding, it will display the current bindings for the selected mode.

Command Classes

Command classes are how you can implement multi-key bindings in screen. Basically, screen has to do something for every key the user presses. (Modifiers don't count; C-a is considered a single keypress.) To effect a multi-key sequence, you have to tell screen that the first keypress activates a command class, where that command class contains a binding for the next key, and so on. This is effectively how normal screen keybindings work: the C-a activates the default command class, which contains all of the default bindings, plus any that you've added with bind.

You “activate a command class” with the command command. Without any arguments, it activates the default command class (just like C-a does). Its -c argument activates whatever class is provided to it. Similarly, bind's -c option makes a binding in the specified command class.

(To be fair, command classes are just the most flexible way to implement multi-key bindings in screen. The bindkey command will bind commands to strings as well as single characters. If the characters in the string arrive quickly enough (faster than maptimeout), they will trigger the binding. The -t option will even disable the timeout.)

Quoting

Sometimes you don't want to activate a keybinding. That's where mapdefault comes in; it causes the next keypress to not be checked against the user input translation table (see bindkey Tables above). This bypassess all screen keybindings except the ones needed for its terminal emulation. If you need to bypass the default input translation table too, use mapnotnext.

Faking Keypresses

The stuff command will take a string and send it directly to the current window. See the Examples section for some places where it's useful.

Keybindings

  • C-a a - send a literal C-a to the window.
  • C-a ? - display the current set of keybindings.

Commands

  • bind - Binds a command to a key as a standard screen command.
  • bindkey - Binds a command to an arbitrary keypress in any of screen's input translation tables.
  • command - Activates a command class.
  • help - Displays the current keybindings.
  • mapdefault - Prevents the next keypress from being looked up in the user input translation table.
  • mapnotnext - Prevents the next keypress from being looked up in any input translation table.
  • maptimeout - Sets the inter-character timeout for multi-character keybindings.
  • markkeys - Changes the keybindings for copy mode.
  • escape - Changes screen's command prefix character.
  • meta - Sends the screen command prefix character to the current window.
  • stuff - Sends a string to the current window.

Examples

Different Prefix Keys

vi users might want to use the escape character as their prefix key. When doing that, you have to decide whether two escapes in a row will call other (to switch to the most recently visited window) or meta (to send an escape character to the window). Here's an example of each:

# ESC ESC calls meta
escape ^[^[
# ESC o calls other
bind o other
# ESC [ calls meta
escape ^[[
# ESC ESC calls other by default

emacs users will probably want to remap the escape character just because it interferes with C-a in emacs. One option is to use C-z instead, as it's close to C-a and screen lets you open new programs in new windows, rather than having to suspend the current program first:

escape ^zz

bindkey Examples

The default input translation table is for terminal behavior. To deal with a terminal that wasn't sending the right character for its backspace button, you could use screen to “fix” the keypress:

bindkey -d -k kb stuff "\177"

The copy mode input translation table can be used to automate things in copy mode. This causes C-g to find the first occurrence of “foo” in the buffer and copy the entire line it's on:

bindkey -m ^G stuff "g/foo\012Y"

Command Class Examples

You can use the default command class to make a second escape key. Suppose you want both C-z and C-\ to be escape keys. You could do it like this:

escape ^zz
bindkey ^\ command

Let's say you wanted to to make the split commands a little more mnemonic and use C-a s h and C-a s v for “split horizontally” and “split vertically”, respectively. Here's something that would work:

bind s command -c split_class
bind -c split_class h split
bind -c split_class v split -v

The bind page shows some further examples that use command classes to make bindings to select windows numbered higher than 9.

Quoting Examples

It's often useful to have a quote key that sends the next keypress through unintercepted. Here's how to accomplish that:

bind ^Q mapdefault

With that, you can press C-a C-q another-key and another-key will be passed through to the window (unless it's affected by the default input translation table, but you generally want those to still be mapped, because they affect screen's terminal definitions).

emacs-Friendly copy Mode

The keybindings in copy mode are mostly based on vi keys (although it has emacs-style incremental searching). Here's a remapping that makes some of the keys closer:

markkeys j=^N:k=^P:l=^F:0=^A:$=^E:^F=^V
bindkey -m -t ^[v    stuff ^B
bindkey -m    ^B     stuff h
bindkey -m -t ^[a    stuff \^
bindkey -m -t ^[f    stuff w
bindkey -m -t ^[b    stuff b
bindkey -m -t ^[0^[r stuff H
# Nothing for 'M'
bindkey -m -t ^[-^[r stuff L
bindkey -m -t ^[<    stuff g
bindkey -m -t ^[>    stuff G
bindkey -m -t ^[d    stuff " e "

markkeys is used for the single-character bindings, while bindkey is used for the multi-character keys. (You can't specify a meta key, but you can use the combination with escape that is equivalent. xterm can even be set up to send an escape character when a meta-key combination is pressed.)

A more complete mapping is left as an exercise for the reader.

xterm-Style Scrolling

In xterm, shift-PgUp and shift-PgDn move through the terminal's scrollback buffer. screen keeps a separate scrollback buffer for each window, so this example lets you use those keypresses to move around screen's scrollback buffer.

First, you need to disable scrolling in xterm, so this goes in your .Xresources file:

XTerm.vt100.translations: #override \n\
    Shift <Key>Prior:string(0x1b) string("[5;2~") \n\
    Shift <Key>Next:string(0x1b) string("[6;2~")

Then you need to make shift-PgUp enter copy mode and go back a page, and both key combinations move around in copy mode. This goes in your .screenrc:

bindkey "^[[5;2~" eval "copy" "stuff ^u"
bindkey -m "^[[5;2~" stuff ^u
bindkey -m "^[[6;2~" stuff ^d

User Tools