The Wild

Cat Command Output: Syntax Highlighting or Color?

· 5 min read · Mammals

In this article
  1. Why Plain cat Has No Color
  2. Best Alternatives: bat, ccat, and Others
  3. How to Enable Color Without Installing Anything
  4. Choosing the Right Tool for Your Workflow
  5. Common Problems and Fixes
  6. Safety and Performance Considerations
  7. Frequently Asked Questions

Yes, you can get syntax highlighting or color in cat output, but the plain cat command itself doesn't do it—you need to use alternatives like bat, ccat, or configure highlight or pygmentize to pipe into cat. This article explains the options, how they work, and which one fits your workflow.

Why Plain cat Has No Color

The standard cat (concatenate) command is a minimal Unix utility that reads files and writes them to standard output. It has no built-in syntax highlighting or color support because its job is simply to dump bytes, not to interpret them. Colorizing output would require parsing the file's content, which is outside cat's scope. This is intentional—cat is designed to be fast, scriptable, and dependency-free.

However, terminal emulators can add color to any text via ANSI escape codes. Tools that provide syntax highlighting generate these codes and output them, so when you see colored cat output, it's actually another program doing the work, either replacing cat or wrapping it.

Best Alternatives: bat, ccat, and Others

The most popular drop-in replacement is bat, a clone of cat with syntax highlighting, line numbers, and git integration. You can install it via your package manager (e.g., apt install bat on Debian/Ubuntu, brew install bat on macOS). Once installed, you can alias cat to bat in your shell config:

alias cat=bat

Other options include:

  • ccat – a lighter alternative that colorizes output but doesn't add line numbers by default.
  • highlight – a versatile tool that can format to ANSI, HTML, or LaTeX; use highlight -O ansi file.py.
  • pygmentize – from the Pygments library, supports many languages: pygmentize -g file.py.

Each has its strengths: bat is feature-rich, ccat is simple, highlight and pygmentize are more flexible for scripting.

How to Enable Color Without Installing Anything

If you can't install new tools, you can still get colored output using existing utilities. For example, grep can colorize its matches, but not the whole file. A common trick is to use less with syntax highlighting:

less -R file.py

But that doesn't help with cat. Another approach is to use vim in read-only mode: vim -R file.py. For a pure cat-like experience, you can write a simple shell function that uses pygmentize if available:

catc() { pygmentize -g "$1" 2>/dev/null || cat "$1"; }

This function falls back to plain cat if pygmentize isn't installed.

Choosing the Right Tool for Your Workflow

Consider these factors:

  • Frequency of use – If you view code daily, installing bat is worth it.
  • Scripting – In scripts, you often want plain cat to avoid ANSI codes messing up pipelines. Reserve colored output for interactive use.
  • Performance – cat is instant; bat and highlight add parsing overhead, noticeable on huge files.
  • Customization – bat supports themes; highlight lets you define your own color schemes.

For most users, bat is the best balance of features and ease. If you only need occasional color, a function with pygmentize is lighter.

Common Problems and Fixes

Issue: Colors don't show up. This usually means the terminal doesn't support ANSI colors or the tool isn't outputting them. Check with echo -e "\e[31mred\e[0m" – if you don't see red, your terminal is set to monochrome. Also, some tools (like bat) automatically disable color when output is piped, not a terminal. Use bat --color=always to force it.

Issue: Line numbers appear but no color. This can happen if the file type isn't detected. Bat uses the file extension; if it's unknown, it defaults to plain. Use bat -l python file to specify the language.

Issue: Aliases not working. Ensure you've restarted your shell or sourced your config file. Also, some systems have batcat instead of bat (Debian/Ubuntu) – adjust the alias accordingly.

Safety and Performance Considerations

Using colored cat is safe; it doesn't modify files. However, be cautious when piping colored output to other commands. For example, cat file.py | grep foo will include ANSI codes in the grep output, which can be confusing. Strip colors with sed 's/\x1b\[[0-9;]*m//g' if needed.

Performance-wise, syntax highlighting is CPU-intensive for large files. Bat has a --paging option to avoid loading huge files entirely. For files over a few megabytes, plain cat is faster and more reliable.

Frequently Asked Questions

Can I make the original cat command show colors?

No, the original cat has no color support. You must use a replacement or wrapper. The alias approach is the simplest way to get color while still typing cat.

Does bat work on all file types?

Bat supports many languages out of the box, but not all. For unsupported types, it falls back to plain text. You can add custom syntax definitions via its configuration file.

How do I turn off colors when I don't want them?

Use cat directly (bypassing the alias) or call the tool with a flag: bat --color=never, ccat --color=never, or pygmentize -f terminal -O style=monokai (but that's for color). For bat, you can also set an alias alias cat-plain=/bin/cat.

Is there a way to colorize only specific parts of a file?

Yes, grep --color highlights matches, but not syntax. For partial syntax highlighting, you'd need a tool that supports ranges, like bat --line-range=10:20 file.py.

Does syntax highlighting work over SSH?

Yes, as long as your remote terminal supports ANSI colors. The tools run on the remote machine and output codes that your local terminal interprets. Ensure your SSH client and terminal emulator are color-capable.

What's the difference between bat and ccat?

Bat is more feature-rich: line numbers, git integration, paging, and theme support. Ccat is simpler and lighter, focusing only on colorizing output. Bat is actively maintained; ccat is less so.