The cat command is one of the most frequently used tools in the Linux terminal. It displays the contents of a file, and it has been doing so without any changes since the early days of Unix. However, reading source code or configuration files with cat is a painful experience — there is no syntax highlighting, no line numbers, no visual separation between code structures, and long files scroll past so fast that you cannot read them.
bat is a modern replacement for cat that adds syntax highlighting for hundreds of programming languages, automatic line numbers, Git integration (showing which lines have been modified), and intelligent paging for long files. It is written in Rust, which means it is extremely fast, and it works as a drop-in replacement for cat in most workflows.
How to Install bat on Ubuntu
bat is available in the default Ubuntu repositories:
sudo apt update && sudo apt install bat
Important note for Ubuntu users: Due to a package naming conflict with another tool, the bat binary on Ubuntu is installed as batcat instead of bat. To use it as bat, create an alias by adding this line to your ~/.bashrc file:
alias bat='batcat'
Then reload your shell: source ~/.bashrc
Alternatively, you can create a symbolic link:
sudo ln -s /usr/bin/batcat /usr/local/bin/bat
Basic Usage
Using bat is identical to using cat. Simply replace cat with bat (or batcat):
bat filename.py
The file contents are displayed with:
- Syntax highlighting appropriate to the file type (Python, JavaScript, JSON, YAML, Markdown, Bash, C, Rust, and hundreds more).
- Line numbers in a left-hand column.
- A header bar showing the file name and language.
- Git change indicators in the left margin (if the file is in a Git repository).
bat automatically detects the language from the file extension. For files without extensions, you can specify the language manually:
bat --language=json config
Syntax Highlighting in Action
bat supports over 200 programming and markup languages out of the box. Some commonly highlighted formats include:
- Python (.py) — Keywords, strings, comments, function names, and decorators are all distinctly coloured.
- JavaScript / TypeScript (.js, .ts) — Full syntax highlighting including template literals and JSX.
- JSON (.json) — Keys, values, strings, numbers, and brackets are colour-coded, making deeply nested JSON structures readable.
- YAML (.yml, .yaml) — Indentation-based structures are visually clear with distinct key and value colours.
- Bash (.sh) — Variables, commands, pipes, and control structures are highlighted.
- Markdown (.md) — Headers, bold text, links, and code blocks are rendered with appropriate styles.
- HTML/CSS — Tags, attributes, selectors, and properties are all distinctly coloured.
- Configuration files (.conf, .ini, .toml) — Section headers, keys, and values are visually separated.
Git Integration
If the file you are viewing is inside a Git repository, bat automatically shows change indicators in the left margin:
- + (green) — Lines that have been added since the last commit.
- ~ (yellow) — Lines that have been modified.
- – (red) — Indicates where lines have been deleted (shown between existing lines).
This gives you an instant visual overview of what has changed in a file without running git diff.
Automatic Paging
When a file is longer than your terminal window, bat automatically pipes the output through a pager (by default, less). This means you can scroll up and down through the file, search for text with /, and quit with q.
If the file is short enough to fit on a single screen, bat displays it directly without invoking the pager. This intelligent behaviour means bat always does the right thing regardless of file length.
If you want to force bat to output without paging (for example, when piping to another command), use:
bat --paging=never filename.py
Viewing Multiple Files
Like cat, bat can display multiple files in sequence:
bat file1.py file2.py file3.py
Each file gets its own header bar showing the filename, making it easy to see where one file ends and another begins. This is far more readable than cat, which concatenates files without any visual separation.
Using bat with Pipes
bat works seamlessly in pipelines. If bat receives input from a pipe (rather than a file argument), it still applies syntax highlighting. You can specify the language using --language:
curl -s https://api.example.com/data | bat --language=json
This is incredibly useful for viewing API responses, command output, or any data stream with proper formatting.
Customising bat’s Appearance
Themes
bat includes multiple colour themes. View the available themes with:
bat --list-themes
Set a theme temporarily:
bat --theme="Dracula" filename.py
Set a permanent default theme by adding this to your ~/.bashrc:
export BAT_THEME="Dracula"
Popular themes include Dracula, Monokai Extended, Nord, One Dark, and Solarized. Choose one that matches your terminal’s colour scheme for the best readability.
Disabling Decorations
If you want syntax highlighting but not the header bar, line numbers, or grid lines, use the --style flag:
bat --style=plain filename.py— Shows only syntax-highlighted content, no decorations.bat --style=numbers filename.py— Shows line numbers only.bat --style=header,grid filename.py— Shows the header and grid lines but no line numbers.
Setting bat as Your Default cat
Add the following alias to your ~/.bashrc or ~/.zshrc:
alias cat='batcat --paging=never'
The --paging=never flag ensures bat behaves exactly like cat when used in scripts or pipelines that do not expect interactive paging. For interactive use, you can still run bat directly (with your bat alias) to get the full paging experience.
bat vs cat: Comparison
| Feature | cat | bat |
|---|---|---|
| Syntax highlighting | No | Yes (200+ languages) |
| Line numbers | Requires -n flag | Default |
| Git integration | No | Yes |
| Automatic paging | No | Yes |
| File headers | No | Yes |
| Themes | No | Multiple built-in |
| Pipe support | Yes | Yes |
| Performance | Fast | Fast (Rust) |
Once you start using bat, reading files with plain cat feels like reading code on a typewriter. The syntax highlighting alone makes it worth the install, and the Git integration and automatic paging are useful bonuses that save you from running additional commands.