How to Use bat as a Better cat
Learn how to use bat as a modern replacement for cat, with syntax highlighting, line numbers, paging control, and Git diff integration on Linux.
Before you start
- ▸A terminal with colour support (xterm-256color or equivalent)
- ▸sudo or root access to install packages
- ▸Basic familiarity with the command line and cat
bat is a cat clone written in Rust that adds syntax highlighting, line numbers, a built-in pager, and Git integration. It reads the same files and pipes you already use with cat, so adoption is low-friction. This guide walks through installation, the most useful features, and how to wire bat into your daily Git workflow.
Installation
Debian / Ubuntu
The package is called bat on Ubuntu 20.04+ and Debian 11+. On older Debian releases the binary was renamed batcat to avoid a name conflict; handle that with a symlink.
sudo apt update && sudo apt install bat
If your release installs it as batcat, create the symlink now so every example below works unchanged:
mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat
Make sure ~/.local/bin is on your PATH (add export PATH="$HOME/.local/bin:$PATH" to ~/.bashrc or ~/.zshrc if needed).
Fedora / RHEL / Rocky
sudo dnf install bat
Arch Linux
sudo pacman -S bat
Verify the install
bat --version
Output will look like bat 0.24.0 (version varies by distro).
Syntax Highlighting
bat auto-detects the language from the file extension and applies colour themes. Open any source file and the difference from plain cat is immediate:
bat /etc/ssh/sshd_config
To see every language bat recognises:
bat --list-languages
Force a specific language when the extension is ambiguous or missing:
bat --language=yaml deployment.conf
Choosing a colour theme
List available themes:
bat --list-themes
Preview a theme live:
bat --theme="Dracula" ~/.bashrc
Set a permanent default in bat's config file (created on first run or manually):
bat --generate-config-file
Then edit ~/.config/bat/config and add:
--theme="Dracula"
On terminals without true-colour support, base16 or ansi are safe fallbacks.
Line Numbers and Display Options
Line numbers are on by default when writing to a terminal. Control the decorations explicitly with --style:
# Show only line numbers and the file header, no Git gutter
bat --style=numbers,header /etc/fstab
Available style components include numbers, header, grid, rule, changes (Git gutter), snip, and full. Combine them with commas or use plain to strip everything:
# Plain output — identical to cat, useful in scripts
bat --style=plain /etc/hosts
Highlighting specific lines
Draw attention to a range or individual lines — useful when sharing terminal screenshots:
bat --highlight-line 10:20 server.conf
Showing non-printing characters
Equivalent to cat -A; useful for debugging hidden tabs or carriage returns:
bat --show-all suspicious_file.sh
Paging
bat pipes output through less automatically when content is taller than your terminal. The pager is fully configurable.
Controlling pager behaviour
# Disable the pager entirely
bat --paging=never Makefile
# Always page, even for short files
bat --paging=always README.md
The default less flags bat passes keep ANSI colours intact. You can override them:
export BAT_PAGER="less -RF"
Add that line to your shell's rc file to make it permanent. If you prefer more or moar, point BAT_PAGER at either.
Using bat as a man page viewer
bat ships a syntax definition for man pages. Override the MANPAGER environment variable:
export MANPAGER="sh -c 'col -bx | bat -l man -p'"
Put that in ~/.bashrc or ~/.zshrc, then man ls will render with syntax colour and bat's pager.
Git Integration
bat reads Git metadata from the repository containing the viewed file and marks added, modified, and removed lines in the left gutter — the same information git diff knows about, surfaced inline while you read a file.
The Git gutter
The gutter is part of the default style. Open any tracked file with uncommitted changes and you will see coloured markers (+, ~, -) next to modified lines. To make the gutter the only extra decoration:
bat --style=changes src/main.rs
Replacing git diff with bat
Tell Git to use bat as its diff pager so staged and unstaged diffs gain syntax colouring:
git config --global core.pager "bat --paging=always"
For a richer diff experience, pair bat with delta (a dedicated diff viewer that also uses bat's syntax engine), but plain bat as a pager already beats raw less for readability.
Previewing files in fzf with bat
If you use fzf for fuzzy finding, bat makes an excellent preview command:
export FZF_CTRL_T_OPTS="--preview 'bat --color=always --style=numbers --line-range=:100 {}'"
Add this to your shell rc file. Pressing Ctrl+T will now show a syntax-highlighted preview panel alongside the fuzzy finder.
Aliasing bat to cat
A common approach is to alias cat to bat so muscle memory keeps working. Add to ~/.bashrc or ~/.zshrc:
alias cat='bat --paging=never'
--paging=never is important here: a true cat replacement should not hijack your terminal with a pager when you pipe output into another command. With paging off, bat passes through cleanly — cat file | grep pattern keeps working exactly as before. Syntax highlighting is automatically suppressed when stdout is not a terminal, so scripts are unaffected.
Verification
Confirm everything is wired up correctly:
# Check version and config path
bat --version
bat --config-file
# Confirm the theme is applied
bat --theme ~/.bashrc
# Confirm the alias works
type cat
The last command should print something like cat is aliased to 'bat --paging=never'. Open a file inside a Git repository with uncommitted changes and verify the gutter markers appear.
Troubleshooting
No colour output
If the terminal shows plain text with no highlighting, check that TERM is set to a value that supports colour (xterm-256color, tmux-256color, etc.). Force colour explicitly for one-off diagnosis:
bat --color=always /etc/hosts
bat: command not found after install on Debian
The binary is named batcat on some Debian releases. Check with which batcat, then create the symlink shown in the installation section above.
Pager exits immediately
If less quits before you can read the file, the -F flag (quit if output fits on one screen) is likely set in your LESS environment variable. Either remove that flag from LESS, or set BAT_PAGER="less -R" to override it for bat specifically.
Git gutter not showing
The gutter only appears when the file is inside a Git repository and has uncommitted modifications. Confirm with git status. Also verify --style includes changes or is set to full (the default).
Frequently asked questions
- Does bat work correctly in shell scripts and pipes?
- Yes. bat detects when stdout is not a terminal and disables colour and decorations automatically, so bat file | grep pattern works identically to cat file | grep pattern.
- Why is the binary called batcat on my Debian system?
- Older Debian releases had a name conflict with another package, so the maintainers renamed the binary to batcat. Create a symlink at ~/.local/bin/bat pointing to /usr/bin/batcat to use it as bat.
- Can bat display files that cat cannot?
- bat reads standard files and stdin just like cat. It adds presentation on top; it does not change what files are accessible or how they are read.
- How do I stop bat from using a pager when I only want to print a file?
- Pass --paging=never on the command line, or set it permanently in ~/.config/bat/config. The alias cat='bat --paging=never' is the most common approach.
- Does bat work over SSH or in minimal environments without a colour terminal?
- bat respects the TERM variable and the --color flag. Set --color=never if the remote terminal does not support ANSI codes, or use --style=plain to get output identical to cat.
Related guides
Bash Arrays and Associative Arrays
Master bash indexed and associative arrays: declaration, element access, looping, mapfile, namerefs, and practical patterns for real scripting work.
Bash Functions and Variable Scoping
Master Bash function scoping with local variables, source-based libraries, correct use of return codes, and array passing techniques including namerefs.
Bash Loops: for, while and until
Learn all three Bash loop types — for, while, and until — with practical, copy-paste examples covering file iteration, counting, polling, and safe line reading.
Bash Scripting for Beginners
Learn Bash scripting from scratch: shebang lines, variables, conditionals, loops, and arguments, plus a real backup script to tie it all together.