Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

migrated from stackoverflow.com Apr 28 '12 at 5:29

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
2  
I'm piping from git. I'v found that it has similar configuration (eg. git config color.diff always) that keeps colorization. –  Errandir 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 6 '13 at 15:21
add comment

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)

share|improve this answer
    
Is there any way to keep colorization in color=auto mode (to make program think that output goes to console instead of pipe)? –  Errandir Apr 27 '12 at 14:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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