3

Basically I want to animate my terminal but because the text is too long, I need a pager. Let's just say I want to change my text colour periodically in less. Is it possible?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
stevemao
  • 131

3 Answers3

4

EDIT The answers by vonbrand and Thomas Dickey are more technically accurate.

less supports raw ANSI escapes when the -r option is used. It also supports Erase in line.

You won't see any animation though. For example:

echo -e "foo\x1b[1G\x1b[2Kbar" > test.ansi
less -r test.ansi

Will only print bar.

more does pass on ANSI escapes by default but does not support line editing from what I have tested.

See also this Stackoverflow answer on the differences of less, more and most.

kba
  • 823
  • 4
  • 13
3

Clearing the line has only an indirect relationship to changing the terminal colors: if you change the background color, then many terminals (Linux console, rxvt, xterm as well as programs imitating one of those) will color the cleared area of the background using that color.

less does use a few clearing operations, but not \x1b[2K. Reading the source, it uses several features using the termcap interface. The most relevant parts are described in the terminfo(5) manual page:

   clr_bol                   el1    cb   Clear to beginning
                                         of line
   clr_eol                   el     ce   clear to end of line
                                         (P)

That is, \x1b[K and \x1b[K, for el1 and el, respectively. There is no conventional termcap capability el2, and (unless you tell less to just pass-through the content of a file using the -r or -R option), less will not send a \x1b[2K.

The same is true of more, e.g., as in the util-linux package: it uses only features from termcap. Unlike less, more has no option for sending nonprinting characters directly to the terminal.

If you use the -r or -R option of less, you have to keep in mind that it is limited: less does not know (or care, much) what those escapes do. From the manual page:

-r or --raw-control-chars
Causes "raw" control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (octal 001) is displayed as "^A". Warning: when the -r option is used, less cannot keep track of the actual appearance of the screen (since this depends on how the screen responds to each type of control character). Thus, various display problems may result, such as long lines being split in the wrong place.

Thomas Dickey
  • 76,765
2

What reacts to the escape sequences is normally the tty (unless the running program sets it not to honor them, in which case the program itself may do so). more(1) does rather primitive screen rewriting (it is really enough to write out screen length lines, and wait for a keypress), so I'd guess it just passes input through. less(1) needs to back up, so it needs a more detailed control of the screen.

vonbrand
  • 18,253