Difference between less -r and less -R

less

less has two options:

-r or --raw-control-chars

Causes "raw" control characters to be displayed.

-R or --RAW-CONTROL-CHARS

Like -r, but only ANSI "color" escape sequences are output in "raw" form.

Does this mean they are equivalent when there are only color control chars? My output has ^O when piping to less -R but not when piping to less -r. What's happening here?

Best Answer

According to the manpage:

    -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.

   -R or --RAW-CONTROL-CHARS
          Like -r, but only ANSI "color" escape sequences are output in "raw" form.  Unlike -r, the screen appear‐
          ance is maintained correctly in most cases.  ANSI "color" escape sequences are sequences of the form:

               ESC [ ... m

          where  the  "..."  is  zero  or  more color specification characters For the purpose of keeping track of
          screen appearance, ANSI color escape sequences are assumed to not move the cursor.  You  can  make  less
          think  that  characters  other  than  "m" can end ANSI color escape sequences by setting the environment
          variable LESSANSIENDCHARS to the list of characters which can end a color escape sequence.  And you  can
          make  less  think  that  characters other than the standard ones may appear between the ESC and the m by
          setting the environment variable LESSANSIMIDCHARS to the list of characters which can appear.

Apparently, less escapes ANSI escape codes by default, displaying carets (^) followed by codes instead. -r does not escape these characters, so if the input contains random binary data, the console could output unintended gibberish as a result of any ANSI control characters. (That's why less can't know what the screen looks like without processing these chars itself.) -R allows only color control chars through, so output can contain colored text but not other formatting chars that could mess up the output.

Related Question