bat
Cat clone with syntax highlighting and Git integration.
Official WebsiteFeatures
Syntax HighlightingGit IntegrationLine NumbersPaging
Replaces
catInstallation
Homebrew
brew install batAPT (Debian/Ubuntu)
apt install batPacman (Arch)
pacman -S batCargo (Rust)
cargo install batWhy 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 jsonOutput Example
───────┬────────────────────────────────────────
│ File: src/main.rs
───────┼────────────────────────────────────────
1 │ use std::io;
2 │
3 + │ fn main() {
4 + │ println!("Hello, world!");
5 │ }
───────┴────────────────────────────────────────
The + mark on the left indicates lines added by Git.
Common Options
| Option | Description | Example |
|---|---|---|
-n | Display line numbers only (no Git changes) | bat -n file.py |
-A | Show invisible characters | bat -A file.txt |
-l | Explicitly specify language | bat -l python script |
-p | Plain display (no decoration) | bat -p file.txt |
-r | Specify line range | bat -r 10:20 file.rs |
--diff | Show Git diff only | bat --diff file.rs |
--theme | Specify color theme | bat --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.mdCombining 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 diffUse 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 gitConfiguration 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
| Feature | cat | bat |
|---|---|---|
| Syntax Highlighting | - | 200+ languages |
| Line Numbers | -n option | Displayed by default |
| Git Integration | - | Mark changed lines |
| Pager | Manual piping | Automatic (configurable) |
| Show Invisible Characters | -A option | Clearer with -A |
| Speed | Fast | Slightly 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. Settingalias bat=batcatis 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/