4
  • ESC sends \x1b. That's 1 byte: the actual escape character.

  • Page Up sends \x1b[H. That's 3 bytes.

  • F2 sends \x1b[OQ. That's 4 bytes.

  • F5 sends \x1b[15~. That's 5 bytes.

What's the maximum length for one of these? Is this documented somewhere?

  • 1
    It depends on the terminal, doesn't it. man terminfo and man termcap – Ralph Rönnquist Feb 22 '16 at 5:06
  • @RalphRönnquist The termcap database is an obsolete facility but thank you – cat Feb 22 '16 at 5:07
5

There is no predefined limit for the length of control sequences. OP gives as examples some strings sent by special keys, which are documented in XTerm Control Sequences.

xterm starts with a list of possible key codes, may add codes for modifiers as outlined in the Alt and Meta keys section. There is no table of lengths. One complication for doing that is that there are several resource settings which work together to make a few thousand possible keyboard arrangements. Rather than describe all of those, the xterm terminal description is presented as a set of terminfo building blocks (the names with "+"), including user-defined capabilities for modified keys (e.g., control, shift, etc).

  • The terminfo for xterm page lists those (generated by a script).
  • The building-blocks are limited in size, to fit within the 4096-byte limit on compiled terminfo assumed by most implementations.
  • The ncurses terminal database lists a subset of those building-blocks.
  • It also documents the user-defined capabilities used by the xterm entries, noting that there are many more keys possible than are documented.

Some other terminals implement the xterm scheme, but only for particular combinations. So those would be easier to enumerate. They are in a sense "predefined".

However, special keys are not the only type of control sequence. Each of these terminals using the ECMA-48 format accepts control sequences sent from the host. Generally speaking, they accept numeric or string parameters:

  • xterm ignores numbers larger than 65535, so you could take that as a limit on the number of digits (but terminal-dependent).
  • A control sequence could include an arbitrary number of numeric parameters. For example, xterm accepts a control sequence which changes each of the 256 colors in the 256-color palette. That's a few kilobytes (which could be estimated for an upper bound). The control sequence parser does not need more than a few numbers at any point.
  • string parameters (such as setting the title on a window) do not have a predefined limit on their length.

Again, other terminal emulators may use their own limits on the length of control sequences that they accept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.