Terminal GuideTerminal Guide
bat icon

bat

Modern CLI
macOSLinuxWindows
Rust

Cat clone with syntax highlighting and Git integration.

Official Website

Features

Syntax HighlightingGit IntegrationLine NumbersPaging
Replaces
cat

Installation

Homebrew
brew install bat
APT (Debian/Ubuntu)
apt install bat
Pacman (Arch)
pacman -S bat
Cargo (Rust)
cargo install bat

Why Use bat?

bat provides all the functionality of the traditional cat command while offering additional features that significantly enhance developer productivity.

Syntax Highlighting

Supports over 200 programming languages and configuration file formats. File contents are instantly understandable.

Git Integration

Display modified lines with markers. Additions, deletions, and changes are instantly recognizable.

Line Numbers

Displays line numbers by default. Convenient for identifying error lines and code reviews.

Automatic Paging

Long files are automatically displayed with a pager (less). Comfortable scrolling experience.

Basic Usage

Display Files

Basic Commands
# Display a single file
bat README.md

# Display multiple files
bat src/main.rs src/lib.rs

# Read from standard input
curl -s https://example.com/data.json | bat -l json

Output Example

───────┬────────────────────────────────────────
│ File: src/main.rs
───────┼────────────────────────────────────────
1use std::io;
2
3 +fn main() {
4 +println!("Hello, world!");
5}
───────┴────────────────────────────────────────

The + mark on the left indicates lines added by Git.

Common Options

OptionDescriptionExample
-nDisplay line numbers only (no Git changes)bat -n file.py
-AShow invisible charactersbat -A file.txt
-lExplicitly specify languagebat -l python script
-pPlain display (no decoration)bat -p file.txt
-rSpecify line rangebat -r 10:20 file.rs
--diffShow Git diff onlybat --diff file.rs
--themeSpecify color themebat --theme=Dracula file.rs

Practical Usage Examples

Display Specific Line Ranges

Line Range Specification
# Display lines 10-20
bat -r 10:20 src/main.rs

# Display from line 100 onwards
bat -r 100: src/main.rs

# Display first 50 lines
bat -r :50 README.md

Combining with Other Commands

Pipeline Integration
# Display ripgrep results with syntax highlighting
rg -l "TODO" | xargs bat

# Display files found with find
find . -name "*.md" -exec bat {} +

# Highlight git show output
git show HEAD:src/main.rs | bat -l rust

# Make diff output more readable
diff file1.txt file2.txt | bat -l diff

Use as Man Page Replacement

Man Page Display
# Display man pages with bat (after MANPAGER setup)
export MANPAGER="sh -c 'col -bx | bat -l man -p'"
man git

Configuration and Customization

Configuration File

Write default settings in ~/.config/bat/config.

~/.config/bat/config
# ~/.config/bat/config

# Theme settings
--theme="Dracula"

# Tab width
--tabs=4

# Always output color
--color=always

# Display line numbers and Git changes
--style="numbers,changes,header"

# Pager settings
--pager="less -FR"

Check Available Themes

Theme Management
# Display theme list
bat --list-themes

# Preview themes
bat --list-themes | fzf --preview="bat --theme={} --color=always src/main.rs"

Shell Alias Configuration

Alias Configuration
# ~/.bashrc or ~/.zshrc

# Replace cat with bat
alias cat='bat'

# Plain output
alias catp='bat -p'

# Display with line numbers
alias catn='bat -n'

Comparison with cat

Featurecatbat
Syntax Highlighting-200+ languages
Line Numbers-n optionDisplayed by default
Git Integration-Mark changed lines
PagerManual pipingAutomatic (configurable)
Show Invisible Characters-A optionClearer with -A
SpeedFastSlightly slower (for features)

Tips

  • When piping or redirecting, plain output is automatically used, so you can safely use it in scripts
  • On Ubuntu, it's installed with the command name batcat. Setting alias bat=batcat is convenient
  • You can update the syntax definition cache with bat cache --build
  • Custom syntaxes and themes can be placed in ~/.config/bat/syntaxes/ and ~/.config/bat/themes/
Written by Dai AokiPublished: 2026-01-20

Related Articles

Explore More