Table of Contents

bat — A cat Clone with Syntax Highlighting

What is bat?

bat is a cat clone written in Rust that adds syntax highlighting, line numbers, Git integration, and automatic paging to your file viewing experience. It’s one of the most popular modern CLI replacements, making reading source code in the terminal a pleasure instead of a chore.

Why Use bat Instead of cat?

  • 🎨 Syntax highlighting for hundreds of programming languages
  • 🔢 Line numbers displayed by default
  • 📂 Git integration — shows modified/added/deleted lines at a glance
  • 📄 Automatic paging with less for long files
  • 🔍 Non-printable character display for debugging
  • 🚀 Drop-in replacement — same usage as cat

Installation

# macOS (Homebrew)
brew install bat

# Ubuntu / Debian
sudo apt install bat
# Note: on some systems the binary is called batcat
# alias bat='batcat'

# Arch Linux
sudo pacman -S bat

# Cargo (Rust)
cargo install bat

# Windows (Scoop)
scoop install bat

# Windows (Chocolatey)
choco install bat

Basic Usage

# Display a file with syntax highlighting
bat file.py

# Display multiple files
bat file1.js file2.ts

# Display with line numbers (default)
bat -n README.md

# Show without paging (like cat)
bat --paging=never config.yaml

# Show specific line range
bat -r 10:30 main.rs

💡 Tips & Tricks

Tip 1: Set bat as Your Default Pager

Make man pages and git diff use bat’s beautiful highlighting:

# Add to ~/.bashrc or ~/.zshrc
export MANPAGER="sh -c 'col -bx | bat -l man -p'"
export PAGER="bat"

# For git diff
git config --global core.pager bat

Tip 2: Use bat with fzf for File Preview

Combine bat with fzf for an interactive file finder with live preview:

# Preview files while searching with fzf
fzf --preview 'bat --color=always --style=numbers --line-range=:100 {}'

Add this to your shell config for a handy preview alias:

alias preview="fzf --preview 'bat --color=always {}'"

Tip 3: Highlight Specific Lines

Draw attention to important lines using the --highlight-line flag:

# Highlight line 42
bat --highlight-line 42 script.sh

# Highlight a range
bat --highlight-line 10:20 app.py

Tip 4: Choose Your Theme

bat comes with many built-in themes. Pick one that matches your terminal:

# List all available themes
bat --list-themes

# Preview all themes on a file
bat --list-themes | fzf --preview="bat --theme={} --color=always /path/to/file.py"

# Set a permanent theme in bat's config
echo '--theme="Dracula"' >> "$(bat --config-file)"

Tip 5: Use bat as a Syntax Highlighter in Other Tools

You can pipe content to bat and specify the language manually:

# Pipe command output with syntax highlighting
echo '{"name": "bat", "type": "cli"}' | bat -l json

# Highlight a script that has no extension
curl -s https://example.com/script | bat -l bash

# View clipboard content with highlighting (macOS)
pbpaste | bat -l python

Tip 6: Show Non-Printable Characters

Debug encoding issues and invisible characters:

# Show non-printable and special characters
bat -A file.txt

# Show tabs as visible characters
bat --show-all config.ini

Tip 7: Create a bat Config File

Persist your preferred settings so you don’t have to retype flags:

# Create the config file
bat --config-file  # shows the config file location
mkdir -p "$(dirname $(bat --config-file))"
touch "$(bat --config-file)"

# Add your preferences
cat >> "$(bat --config-file)" << 'EOF'
--theme="Dracula"
--style="numbers,changes,header"
--italic-text=always
EOF

Tip 8: Integrate bat with tail -f for Log Monitoring

Watch log files with syntax highlighting in real time:

# Live log monitoring with bat
tail -f /var/log/app.log | bat --paging=never -l log

# Or use the --follow flag (bat 0.24+)
bat --follow /var/log/app.log

Advanced Applications

bat as a Git Difftool

Use bat to replace the default git diff pager for colorful diffs:

# Set bat as the global diff pager
git config --global core.pager "bat --paging=always"

# Or just for a single command
GIT_PAGER='bat' git diff HEAD~1

Integrate bat into Your Shell Functions

# A smarter cat: fall back to bat for code files, plain cat for others
smart_cat() {
  if [ -t 1 ]; then
    bat "$@"
  else
    cat "$@"
  fi
}
alias cat='smart_cat'

bat in Scripts with Plain Output

When using bat inside shell scripts where you don’t want color codes:

# Disable color and decorations for script use
bat --color=never --style=plain filename.txt

# Or set BAT_STYLE and BAT_THEME env vars
BAT_STYLE=plain BAT_THEME=ansi bat file.txt
  • fzf — fuzzy finder that pairs perfectly with bat for file previews
  • vim — when you need to edit, not just view

Real-world Use Cases

  • Code review: quickly preview changed files before committing, with git diff integration to spot context-sensitive changes.
  • Learning new languages: use bat to scan sample projects with syntax highlighting to understand structure faster.
  • Debugging logs: colorize structured logs (JSON, YAML) to spot fields and anomalies.

When Not To Use bat

  • Very small environments where installing Rust binaries is not possible; fallback to cat.
  • Scripts that require pure, colorless output — use --color=never or --style=plain.

Author: CLI Tools Guide — Contact | Last updated: 2024-01-15

Practical Examples: Use bat for More Readable Viewing Workflows

The examples below show how to integrate bat into everyday workflows to improve file viewing and debugging. They cover three scenarios: quick previewing, reviewing Git diffs, and producing colorless output in pipelines.

  1. Quickly preview project files with fzf

Combine bat with fzf to create an interactive file preview tool:

# Select a file with fzf and show a bat preview on the right
fzf --preview 'bat --color=always --style=numbers --line-range=:120 {}' \
  --preview-window 'right:60%' \
  --bind 'enter:accept' \
  --height 40%

This command helps you find files quickly in large projects, with previews that include syntax highlighting and line numbers for easier reading and navigation.

  1. Render Git diffs more clearly

Set bat as Git’s pager to see colorized diffs with line numbers during git diff:

git config --global core.pager "bat --paging=always --style=numbers,changes"

# git diff will now render through bat with color and change markers
git diff HEAD~1

This makes it easier to review changes and locate the exact context of a modification.

  1. Emit plain text in automation scripts for easy parsing

If you want to use bat in CI or scripts without ANSI color codes, disable color and use plain output:

# Use colorless output in scripts so other tools can parse it reliably
bat --color=never --style=plain README.md > /tmp/readme-plain.txt

# Or pipe it directly to grep/awk
bat --color=never src/main.rs | rg "TODO|FIXME"

This preserves bat’s language detection and formatting strengths while keeping the output machine-friendly.

These examples can immediately improve day-to-day use of bat and make file inspection, version review, and automation flows more reliable.

📦 Quick install:cargo install bat
🌐 Official site ↗📅 Published: January 15, 2024