bat β Better cat#
Installation#
sudo apt install bat # Debian/Ubuntu (binary: batcat)
sudo dnf install bat # Fedora/RHEL
brew install bat # macOS / Linux via Homebrew
# Ubuntu alias
alias bat=batcat # add to ~/.bashrc
Basic usage#
bat file.txt # view with syntax highlighting + line numbers
bat file.py func.py # multiple files with headers
bat -n file.txt # show line numbers only (no frame)
bat -A file.txt # show all non-printable characters
bat --plain file.txt # plain output (no decorations), alias: -p
bat -p file.txt | less # pipe to pager manually
Paging#
bat file.txt # auto-pages through $PAGER (less) if tall
bat --paging=never file.txt # never page (like cat)
bat --paging=always file.txt # always page even if content fits
bat -P file.txt # shorthand for --paging=never
Syntax and themes#
bat --list-languages # list all supported languages
bat --list-themes # list all available themes
bat -l python script # force language (no extension)
bat -l json output # treat file as JSON
bat -l sh - # read stdin as bash
bat --theme="Dracula" file.py # apply a specific theme
bat --theme=ansi file.txt # ANSI 16-colour theme (terminal-native)
bat --theme=GitHub file.md # light theme
Set default theme#
# In ~/.config/bat/config (or export BAT_CONFIG_PATH)
--theme="OneHalfDark"
--paging=never
--style=numbers,changes
Style components#
bat --style=full file.txt # all decorations (default)
bat --style=plain file.txt # no decorations
bat --style=numbers file.txt # line numbers only
bat --style=changes file.txt # Git change markers only
bat --style=header file.txt # filename header only
bat --style=grid file.txt # separator lines only
bat --style=numbers,changes,header file.txt # combine
Git integration#
bat highlights modified, added, and deleted lines against the Git index automatically when --style=changes (included in full).
bat --diff file.txt # show only lines that differ from Git HEAD
bat --diff-context=5 file.txt # with 5 lines of context around diffs
Line ranges#
bat -r 10:20 file.txt # lines 10β20
bat -r :50 file.txt # first 50 lines (like head -50)
bat -r 100: file.txt # from line 100 to end (like tail -n +100)
bat -r 1:1 file.txt # first line only
Reading from stdin#
echo '{"key": "value"}' | bat -l json
curl -s https://api.example.com/data | bat -l json
git diff | bat --plain -l diff
Integration with other tools#
# bat as MANPAGER (colourised man pages)
export MANPAGER="sh -c 'col -bx | bat -l man -p'"
export MANRASTER_BIN=cat
# Use bat for previews in fzf
fzf --preview 'bat --color=always {}'
# Use bat in git diff
git config --global core.pager 'bat --paging=always'
git config --global interactive.diffFilter 'bat --plain'
# Use with ripgrep for syntax-highlighted search output
rg --json pattern | bat -l json
# Preview files found by fd
fd -e py | xargs bat --style=header,numbers
# Replace less as a pager
export PAGER="bat --paging=always"
Config file#
Location: ~/.config/bat/config (or $(bat --config-file))
--theme="OneHalfDark"
--paging=auto
--style=numbers,changes,header
--map-syntax="*.config:XML"
--map-syntax="Dockerfile*:Dockerfile"
--map-syntax=".env:Shell Script"
--italic-text=always
Custom syntax mappings#
bat --map-syntax="*.conf:INI" httpd.conf
bat --map-syntax="Makefile*:Makefile" GNUmakefile
Aliases and wrappers#
# ~/.bashrc
alias cat='bat --paging=never'
alias less='bat --paging=always'
alias batdiff='bat --diff'
# Colourised help
alias bathelp='bat --plain --language=help'
help fzf | bat --plain -l help
bat vs cat#
| Scenario | Preference |
|---|---|
| View a code file interactively | bat |
Pipeline (cmd | process) | cat or bat -p |
| Binary/raw file | cat (bat refuses binary) |
| Man pages | bat -l man |
| Quick peeks | bat -r 1:30 |
[!TIP]
bat --config-fileprints the path to your config file.bat --generate-config-filecreates a commented template at that path.