bat — cat with syntax highlighting and no regrets

cat prints text and dares you to read it. bat prints the same text with syntax highlighting, line numbers, and git-aware markers — and knows when to get out of the way.

Bat is what cat would be if it cared about your eyes: it reads a file, detects the language, and prints it highlighted with line numbers and a gutter — plus git change markers in the margin when the file lives in a repo. For human eyeballs this is a straight upgrade; the clever part is that it stays a good unix citizen. When output goes to a pipe or a file, the decoration vanishes and it behaves like plain cat, so grep error $(...) style pipelines don’t suddenly sprout ANSI codes — and if a tool doesn’t detect piping correctly, -p forces plain output. There’s also a pager built in, so a 3,000-line file doesn’t scroll your whole screen into oblivion. That combination — pretty to read, clean to pipe — is why bat slots into aliases and fzf previews everywhere.

install

macOS
# macOS
brew install bat
Debian/Ubuntu
# Debian/Ubuntu (20.04+; binary is batcat on these)
sudo apt install bat
Fedora
# Fedora
sudo dnf install bat
Arch
# Arch
sudo pacman -S bat
Debian/Ubuntu
# Debian/Ubuntu names the binary batcat; alias it once:
echo "alias bat=batcat" >> ~/.bashrc

examples

# Read a file like a human: highlighted, numbered
bat src/main.rs
# Show lines 40-60 of a big file, still highlighted
bat --line-range 40:60 app.log
# Diff two files side by side, highlighted
bat --diff file-v1.conf file-v2.conf
# Which languages can it highlight?
bat --list-languages
# Plain text, always — no pager, no ANSI, no decoration
bat -p robots.txt
# Set a specific language when detection guesses wrong
bat -l bash install.sh.txt
# Use bat as man's pager — highlight, search, all of it
export MANPAGER="sh -c 'col -bx | bat -l man -p'"

pro tip

bat’s non-printing-character view is a debugging superpower: bat -A shows tabs as ->, trailing spaces, and other invisibles right in the gutter. "Why does this YAML fail to parse?" is usually a tab wearing a trench coat, and bat -A catches it in one glance.

honest limits

Bat is a file viewer, not an editor — pipe to less or open your editor when you need to change things. Piped output is plain by design, so don’t expect colors to survive into a pipeline (that’s the point, but it surprises people; -p keeps it plain, --color forces color). On Debian/Ubuntu the binary is batcat, so scripts calling bat break on those boxes. And in scripts, reach for cat: bat detects languages per-file and highlights only what a human will read.