47

Is there any way to keep colorization of text passed through pipe | to head, tail, less, etc.?

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
Timofey Gorshkov
  • 574
  • 1
  • 4
  • 8
  • Any specific output program? It depends on the program sending the data through the pipe. `head`, `tail`, etc aren't the ones removing the colors, it's the program generating the data that usually check if the output is going to the console (colored), a file or pipe (not colored). – KurzedMetal Apr 27 '12 at 12:56

4 Answers4

27

I presume you are piping from ls and want to preserve the terminal color codes. You can say ls --color=always (instead of the default of --color=auto), which will preserve the codes, but that won't guarantee that the thing you're piping to knows how to understand them.

If you use glark instead of grep it will try to display with colors.

If you use less with -R it will attempt to display with colors.

phogg
  • 1,037
  • 9
  • 12
  • 9
    I'm piping from `git`. I'v found that it has similar configuration (eg. `git config color.diff always`) that keeps colorization. – Timofey Gorshkov Apr 27 '12 at 13:59
  • 1
    @Errandir the problem with that configuration is that it will break any script or command that use `git diff` output as input because it will always output the ANSI escape codes. – KurzedMetal Jun 14 '12 at 13:14
  • @Errandir, thanks ! I do not have any scripts which rely on a clean output from git diff so this works for me !! Thank you ! – Ashutosh Jindal Nov 06 '13 at 15:21
  • 2
    I used this for grep: `grep "string" file.txt | head` to make sure the result from grep was colored I just changed this to `grep "string" file.txt --color=always | head` – Emil Stenström Jul 12 '17 at 13:23
  • 2
    `git branch` has a `--color=always` option too. – Noumenon Aug 12 '20 at 15:55
  • It seems that various `git` subcommands have different ways to enable this: A separate answer dedicated to coloring `git` output: https://stackoverflow.com/a/18304605/245966 – jakub.g Jan 14 '22 at 20:20
22

It depends on the program that generate the output in the pipe.

head, tail, etc aren't the ones removing the colors, it's the program generating the data that usually check if the output is going to the console (colored), a file or pipe (not colored)

I found another SU Q&A showing how to lie to piping programs to output as if they were sending output to a console (emulating a console with unbuffer)

KurzedMetal
  • 366
  • 1
  • 8
0

Consider

bat

An alternative to

cat

Install

brew install bat

--args --flags like

--language and --theme

Examples

head | bat --language=zsh -n -p --theme=gruvbox-dark
file.py | bat --language=py -n -p --theme=gruvbox-dark
jasonleonhard
  • 329
  • 3
  • 10
  • 1
    In all fairness, it is not preserving color, it's adding new color based on whatever language its piped and theme assigned. – jasonleonhard Oct 08 '21 at 03:44
0

On Mac OS X man ls mentions the CLICOLOR_FORCE environment variable.

Adding export CLICOLOR_FORCE=1 to ~/.zshrc or ~/.bashrc, depending on the shell you use, keeps the colors when piping ls to other commands like less, head, and tail.

callistix
  • 1
  • 1