Skip to content

How to remove or display ANSI color codes in a log file

Log Viewer
By Underrated SoftwareUpdated

Strings such as ESC[32m, ESC[0m and [36m are ANSI escape codes that set terminal colours and styles. A terminal interprets them, but a plain text editor displays the underlying characters. You can render the colours, remove the codes from a copy, or use a log viewer that interprets them.

Display ANSI colours with less -R

less -R passes color escapes through raw, so the colors render:

less -R compose.log

cat compose.log renders too - your terminal is doing the interpreting - but it scrolls straight past and leaves you with the buffer.

Strip common ANSI colour codes with sed

If you need clean text - to diff it, grep it, or paste it somewhere - the usual answer is sed:

sed 's/\x1b\[[0-9;]*m//g' compose.log > clean.log

This removes the common color codes. It is also lossy and slightly fragile: you now have a second file, and escape sequences beyond simple colors can slip through. Fine as a one-off; annoying as a habit.

Render ANSI colours in a log viewer

Log Viewer treats escape codes as what they are: formatting. It paints the colors the log asked for and takes the codes out of the text - on a compose log that is most of the window's width back at a stroke. Prefer plain? Settings can drop them entirely, or leave them visible if what you are debugging is the codes themselves. Your own highlight rules - errors red, warnings amber - win over what the log asked for, and copying a line gives you the text you could see, not the escape bytes you couldn't.

A log captured from a terminal: the colors painted, the codes gone.
A log captured from a terminal: the colors painted, the codes gone.

Free on the Mac App Store, works entirely offline.

FAQ

Why does my saved log show ESC[0;32m instead of colors?

Programs emit ANSI escape codes to color terminal output. When output is redirected to a file, the codes are saved as bytes; anything that isn't a terminal shows them as text.

How do I strip ANSI codes from a log file?

sed 's/\x1b\[[0-9;]*m//g' handles the common color codes. A viewer that interprets them instead keeps the color and the clean text at once.

Does copying from Log Viewer include the escape codes?

No - copying gives you what you could see, with the codes already removed.

Try Log Viewer

See what Log Viewer does and check whether it fits your task.

FreemacOSDownload on the Mac App Store (opens in a new tab)