Zsh – How to Redirect Colored Output to a File on macOS

colorsmacosredirectionzsh

I want to store a colored terminal output to a file (maybe a .rtf or whatever).
In OSX I am able to manually copy it and paste it in a .rft file using Textedit, however I wanted to store it automatically using something like:

command &> somefile.rtf

Storing the output in a .txt preserves the color tags but they are useless since they can only be seen from the terminal (using cat).

Is there a way to do that?

I read a similiar question but it didn't solve the issue of storing the output in a file format that supports colors (and can be opened without a terminal).

Best Answer

Since the colours are stored with their ansi sequence you can create a script to translate each of them... or you can use a script just made.

For example it exists the script ansi2html [v0.15,v0.23] that you can make executable, put in a directory of your path (typically ~/bin) and use with

command | ansi2html.sh > file.html

Then if you need a different format you can convert it or you can rewrite the script.

Note that some commands know if their output is piped or not.
For example, ls may change its output if piped.

Related Question