bat Cat Replacement

bat cheatsheet — view files with syntax highlighting, line numbers, git diff markers. bat --style=full, bat --theme=TwoDark, bat --pager=never. The cat upgrade.

5 min read

What it is

bat is a cat clone with syntax highlighting and Git integration, useful for viewing file contents with enhanced readability in the terminal.

Installation

Linux (Debian/Ubuntu)

sudo apt update
sudo apt install bat

Linux (Fedora)

sudo dnf install bat

Linux (Arch Linux)

sudo pacman -S bat

macOS

Using Homebrew:

brew install bat

Windows

Using Scoop:

scoop install bat

Using Chocolatey:

choco install bat

Core Concepts

Paging

By default, bat pipes its output to a pager (like less) if the output exceeds the terminal height. This allows you to scroll through large files.

Git Integration

bat can detect if a file is part of a Git repository and show modifications (added, modified, deleted lines) using Git’s diff output.

Syntax Highlighting

bat automatically detects the language of the file and applies syntax highlighting based on common file extensions and shebang lines.

Line Numbers

Line numbers are displayed by default, aiding in code navigation and referencing.

Commands / Usage

Displaying a Single File

bat README.md

Display the content of README.md with syntax highlighting and line numbers.

bat --style=numbers,changes src/main.rs

Display src/main.rs with only line numbers and Git modification indicators.

Displaying Multiple Files

bat file1.txt file2.js

Concatenate and display file1.txt and file2.js with highlighting and line numbers.

Displaying from Standard Input

ls -l | bat

Pipe the output of ls -l to bat for syntax highlighting (detects shell script output).

cat config.yaml | bat

Pipe the content of config.yaml to bat for YAML syntax highlighting.

Specifying Language

bat --language rust Cargo.toml

Force bat to interpret Cargo.toml as Rust code for highlighting.

bat --language json data.txt

Force bat to interpret data.txt as JSON code.

Controlling Styles

bat --style=plain README.md

Display README.md without any decorations (line numbers, Git info, etc.).

bat --style=header,grid README.md

Display README.md with a header and grid lines.

bat --style=full README.md

Display README.md with all available decorations (line numbers, Git info, file name header, etc.).

Showing Git Diffs

bat --diff main.py

Display main.py and highlight lines that have been added, modified, or deleted since the last commit.

Paging Control

bat --paging=never README.md

Display README.md without using a pager, even if the content is large.

bat --paging=always README.md

Always use a pager for README.md, regardless of content size.

Highlighting Specific Lines

bat -H 10 README.md

Display README.md and highlight line 10.

bat -H 5:15 README.md

Display README.md and highlight the range of lines from 5 to 15.

bat -H 10,25,30:40 README.md

Display README.md and highlight lines 10, 25, and the range from 30 to 40.

Showing Only Git Diffs (No File Content)

bat --diff --only-header main.py

Display only the Git diff header for main.py (e.g., "++ modified").

Ignoring Git Diffs

bat --ignore-git-diff README.md

Display README.md but do not show any Git modification indicators.

Specifying Theme

bat --theme 'TwoDark' README.md

Display README.md using the TwoDark color theme.

bat --list-themes

List all available color themes.

Showing File Names

bat --show-files README.md README.txt

Display README.md and README.txt, showing their file names as headers.

Suppressing Line Numbers

bat --no-numbers README.md

Display README.md without line numbers.

Suppressing Git Modifications

bat --no-git-aliases README.md

Display README.md without Git modification indicators.

Suppressing File Name Headers

bat --no-file-headers README.md

Display README.md without the file name header.

Suppressing All Decorations

bat --plain README.md

Equivalent to bat --style=plain README.md.

Common Patterns

Viewing Log Files with Highlighting

tail -f /var/log/syslog | bat

Stream log file output and apply syntax highlighting.

Diffing Two Files with bat

diff -u file1.txt file2.txt | bat

Pipe the unified diff output of two files into bat for highlighted diff viewing.

Showing Git History for a File

git log --pretty=format:"" --name-status | grep ' M\s' -B 1 | bat

Show modified files in the Git history with bat highlighting.

Viewing Configuration Files

bat ~/.config/nvim/init.vim

View your Neovim configuration with Vim syntax highlighting.

Quickly Previewing a File

bat --style=numbers,changes,header my_script.sh

A common setup for quickly previewing scripts with context.

Combining Paging and Highlighting for Large Files

bat --paging=auto --style=numbers,changes large_log_file.log

This is the default behavior, ensuring readability for large files.

Gotchas

Pager Interaction

If bat uses a pager (like less), you interact with the pager, not bat directly. Press q to quit the pager.

Language Detection Failures

For files with unusual extensions or no clear language indicators, bat might guess incorrectly or apply no highlighting. Use --language to force the correct language.

Git Diffs on Non-Git Files

If you run bat --diff on a file that is not part of a Git repository, it will simply display the file content without any Git indicators.

Performance with Very Large Files

While bat is generally fast, extremely large files might still take a moment to load and highlight, especially if Git diffing is enabled.

Binary Files

bat is designed for text files. Attempting to display binary files will likely result in garbled output or errors.

Theme Availability

Ensure the theme you specify with --theme is installed or available in your bat configuration. If not, it will fall back to a default theme.