In the terminal, cat is a basic tool for displaying file contents, but it lacks syntax highlighting and modern features. Enter bat, a powerful replacement that brings beautiful syntax highlighting and more to your command line, transforming how you view files.
Why Replace cat with bat?
The classic cat command is simple and reliable, but it doesn’t highlight code syntax or show line numbers. When debugging or reviewing code, this can make it hard to read. bat solves this by adding syntax highlighting, line numbers, and Git integration, making it ideal for developers.
Here’s what bat brings to the table:
- Syntax highlighting for over 100 programming languages, making code easier to scan
- Line numbers by default (configurable) for quick reference
- Git integration that highlights changes in the file (if in a Git repo)
- Automatic paging for large files (like less), so you don’t get spammed with output
- Customizable themes to match your terminal preferences
Unlike cat, which is designed for simple concatenation, bat is built for human-readable file display. This small change can save hours of debugging time.
Installing bat on Your System
Installing bat is straightforward across platforms. Here are the most common methods:
- macOS: Use Homebrew:
brew install bat - Linux (Debian/Ubuntu):
sudo apt install bat - Linux (RHEL/Fedora):
sudo dnf install bat - Windows: Use Chocolatey:
choco install bator Scoop:scoop install bat
After installation, verify with bat --version. On most systems, you’ll also get a bat man page for detailed documentation.
Basic bat Usage for Syntax-Highlighted Output
Using bat is as simple as replacing cat with bat. For example:
bat filename– Display with syntax highlighting and automatic pagingbat -n filename– Show line numbers (equivalent tocat -nbut with colors)bat --plain filename– Output without any formatting (for scripts)bat --style=full filename– Show line numbers, file header, and grid borderbat --theme=Monokai filename– Use a specific color theme
By default, bat uses a color scheme that’s easy on the eyes and supports over 100 programming languages. For a full list of themes, run bat --list-themes.
Advanced bat Features and Configuration
For deeper customization, set environment variables or create a config file. To make bat your default pager:
- Add to your shell config:
export BAT_PAGER="less -R" - Set as the default for git diff with
git config --global core.pager "bat --paging=never"
You can also create a config file at ~/.config/bat/config to set default styles. For example:
style = "auto"
line-numbers = true
grid = true
theme = "TwoDark"
This configuration ensures consistent output across all bat commands. The style option can be set to auto, full, header, or plain for different levels of detail.
When Not to Use bat
While bat is excellent for interactive use, avoid it in scripts where you need raw output. For example, if you’re piping to another command that expects plain text, bat adds ANSI escape codes which might break the pipeline. In such cases, use cat or bat --plain to disable formatting. Additionally, for extremely large files (over 100MB), bat might be slower than cat due to its syntax parsing. For these scenarios, stick with cat or use bat --plain --paging=never for faster output.
Integrating bat into Your Daily Workflow
Once installed, bat can replace cat in many common scenarios. For example:
- Viewing log files:
bat /var/log/syslog(with syntax highlighting for log lines) - Inspecting configuration files:
bat ~/.bashrc(with shell syntax highlighting) - Reading documentation:
bat README.md(with Markdown rendering) - Checking Git diffs:
git diff | bat(for syntax-highlighted diffs)
By default, bat uses less as a pager for large files. This means you can scroll through the output without it flooding your terminal. To disable paging for a single command, use bat --paging=never filename.
Conclusion
Replacing cat with bat is a simple yet transformative change for terminal workflows. With syntax highlighting, line numbers, and Git integration, bat makes file inspection faster and more intuitive. Start using it today by installing and setting up your preferred configuration. For a more efficient terminal experience, consider pairing bat with tools like fzf for fuzzy searching or exa for modern directory listings.