Diagnosing Missing ANSI Colors in less: A Practical Troubleshooting Guide
When less shows plain text even though the source contains ANSI escape codes, the problem usually lies in the command line options, environment variables, or terminal capabilities. This guide walks through a diagnostic table, ordered checks, and fixes to restore color output.
30 Sept 2025, 13:23 UTC

Problem Statement
Running less on a file or a pipe that contains ANSI escape sequences often results in plain text output. The color information is silently dropped, making logs, diffs, or command output hard to read. This guide helps you identify why the color is missing and how to restore it.
Diagnostic Table
| Condition | Likely Cause | Diagnostic Check |
|---|---|---|
| Less shows no color at all. | Missing -R or -r flag. | Inspect command line or $LESS variable. |
| Color missing only when less is used via a pipe. | Upstream command strips color or lesspipe removes escapes. | Run upstream command directly; check LESSOPEN. |
| Color works in some terminals but not in others. | Terminal emulator reports limited color support or $TERM is restrictive. | Echo $TERM and run tput colors. |
Color still missing after adding -R. | Less compiled without raw character support or terminal lacks ANSI handling. | Run less --version and test with a known‑good color stream. |
Ordered Checks and Fixes
-
Verify the less invocation
Check if
lessis being called with-Ror-r. These flags telllessto pass through raw ANSI codes instead of filtering them.# Show the exact command line used ps -o args= -p $(pgrep -f 'less') # Or, if you are running it manually less -R file.txtIf the flag is missing, add it explicitly or set the environment variable:
LESS='-R' less file.txt # or permanently export LESS='-R' -
Check the
LESSenvironment variableRun:
echo $LESSEnsure it contains
-Ror-r. If it does not, add it temporarily for the current session:LESS=-R less file.txt -
Inspect upstream commands when using pipes
Some commands (e.g.,
git log) automatically strip color when their output is not a terminal. Test the upstream command without piping:git log --color=always | head -5If you see colors, pipe it to
less -Rand confirm:git log --color=always | head -5 | less -RIf colors are absent, the upstream command itself may be suppressing them. Use
--color=alwaysor equivalent flags. -
Examine
LESSOPENandlesspipeThe
lesswrapper scriptlesspipecan preprocess files. It may remove ANSI codes. Check ifLESSOPENis set:echo $LESSOPENIf it points to
lesspipe, temporarily unset it:unset LESSOPEN less -R file.txt -
Verify terminal color capability
Run:
echo $TERMThen check the number of colors supported:
tput colorsValues below 8 typically mean limited or no color support. If the terminal reports
dumbor similar, colors will be disabled. Use a terminal that supports 256 colors (e.g.,xterm-256color) and setTERMaccordingly. -
Confirm less supports raw characters
Run:
less --versionLook for the
rawfeature flag. If absent, you may need to install a fulllesspackage or recompile with--enable-raw. -
Test with a controlled color stream
Use a simple echo command to ensure
less -Rrenders colors:echo -e '\e[32mgreen\e[0m' | less -RIf
greenappears in green, the terminal andlessare functioning correctly. If not, revisit the previous steps. -
Escalation Criteria
- Colors still missing after applying
-Rand ensuringTERMsupports 256 colors. - Upstream commands do not emit color even with
--color=always. - Terminal reports
tput colors< 8 and cannot be changed. - The
lessbinary is custom or stripped down and lacks raw support.
At this point, consider switching to a different pager (e.g.,
mostorbat) or updating your terminal emulator. - Colors still missing after applying
Risk & Considerations
- Using
-Rwill display raw escape sequences as visible characters if the input does not contain ANSI codes. This can clutter the output. - Modifying the global
LESSvariable can affect scripts that rely on default behavior. Prefer per‑command flags or a wrapper script. - Some distributions ship a minimal
lessthat lacks raw support. Checkless --versionand install the full package if necessary.
Practical Verification Checklist
- Run
echo -e '\e[31mred\e[0m' | less -Rand confirmredappears in red. - Check
echo $LESScontains-R. - Run
tput colorsand ensure the value is at least 8. - Run
less --versionand confirmrawsupport is listed. - Verify upstream command emits color with
--color=alwayswhen not piped.
Conclusion
Missing ANSI colors in less typically stem from missing -R flags, upstream suppression, or terminal limitations. By following the ordered checks above, you can pinpoint the exact cause and apply the appropriate fix, restoring a colorful and readable output experience.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.