Pipe to less but keep the highlighting

33

7

Is it possible to pipe output (e.g. dmesg) to a command like less (or equivalent) and keep the text highlighting used by the original command?

example: on the left dmesg | less on the right dmesg

<code>dmesg | less</code> vs <code>dmesg</code>

apoc

Posted 2015-07-13T16:02:02.750

Reputation: 543

Answers

38

Use the --human parameter to view colored dmesg output in a less-like environment.

dmesg --human --color=always

Alternatively, use the following command to achieve similar results.

dmesg --color=always | less -R

Many other utilities which produce colored output (ls, grep, etc.) have a similar --color=always option.

Steven

Posted 2015-07-13T16:02:02.750

Reputation: 24 804

1Actually --human does more than just preserve color and pipe to less: it also marks dates as e.g. [May23 00:58] and subsequent small offsets as [ +6.046768] instead of what would always be [121187.191521] with less. – Ruslan – 2017-05-23T12:56:55.680

Is there a way to make this preserving of color formatting automatic for anytime I'm piping any txt based command (not just dmesg)? – mikemtnbikes – 2018-08-02T18:48:11.700

@mikemtnbikes You have to use the --color=always if provided by the program. A program knows if it is outputting to a pipe and can therefore decide to output color or not. – Steven – 2018-08-22T16:04:42.443