Print a file
Introduction
bat prints files with syntax highlighting, line numbers, and Git change markers. It pages long output and behaves like cat when piped.
- bat README (github.com)
- Customization guide (github.com)
Show a file
bat hello.py
Output
─────┬──────────────────────────────────
│ File: hello.py
─────┼──────────────────────────────────
1 │ def greet(name):
2 │ message = f"Hello, {name}!"
3 │ print(message)
4 │ return message
─────┴──────────────────────────────────
The header, line numbers, and colors appear on a terminal; color is omitted here. Piped output is plain text.
See: Syntax highlighting
Read from a pipe
printf '{"ok": true}\n' | bat -l json
Output
─────┬──────────────────────────────────
│ STDIN
─────┼──────────────────────────────────
1 │ {"ok": true}
─────┴──────────────────────────────────
bat labels piped input STDIN. -l sets the language when there is no filename to infer one from; piping bat's own output drops the decorations.
See: File concatenation
Print several files
bat hello.py config.yml
Each file gets its own header; the contents are concatenated in order.
See: bat --help (FILE)
Highlighting
Set the language
bat -l json config.json
bat --list-languages
bat --file-name hello.py < snippet
-l accepts a language name or file extension. Use --file-name with stdin so bat can infer the language.
See: Syntax highlighting
Set the theme
bat --theme=TwoDark hello.py
BAT_THEME=TwoDark bat hello.py
bat --list-themes
The theme follows the terminal's light or dark background by default. BAT_THEME makes a choice permanent.
See: Highlighting theme
Highlight lines
bat -H 3 hello.py
bat -H 2:4 hello.py
bat -H :3 hello.py
-H draws a background behind the selected lines. It accepts line numbers and ranges such as N:M, :N, N: and N:+M.
See: bat --help (--highlight-line)
Display options
Numbers and plain text
bat -n hello.py
bat -p hello.py
bat --style=plain hello.py
bat -pp hello.py
-n shows only line numbers. -p is shorthand for --style=plain; -pp also disables paging.
See: bat --help (-n, -p)
Style components
bat --style=numbers hello.py
bat --style=numbers,changes,header \
hello.py
bat --style=default,-grid hello.py
Components
| Component | Effect |
|---|---|
numbers | Line numbers |
changes | Git change markers |
header-filename | Filename above the content |
header | Alias for header-filename |
grid | Lines around the sidebar |
snip | Separator between ranges |
--style replaces the default set. Prefix components with + or - to adjust it instead.
See: Output style
Show whitespace
bat -A config.yml
Output
─────┬──────────────────────────────────
│ File: config.yml
─────┼──────────────────────────────────
1 │ name:·bat␊
2 │ langs:·[rust,·python]␊
3 │ enabled:·true␊
─────┴──────────────────────────────────
-A marks spaces, tabs, and line endings. --tabs 2 sets the displayed tab width.
Handle long lines
bat --wrap=character hello.py
bat -S hello.py
bat --terminal-width 80 hello.py
--wrap=character wraps long lines; -S chops them. --terminal-width overrides the detected width.
See: bat --help (--wrap, -S, --terminal-width)
Output views
Print a range
bat -r 2:4 hello.py
Output
─────┬──────────────────────────────────
│ File: hello.py
─────┼──────────────────────────────────
2 │ message = f"Hello, {name}!"
3 │ print(message)
4 │ return message
─────┴──────────────────────────────────
N:M prints a range; :40 starts at the top, 40: runs to the end, and 30:+10 prints lines 30 through 40.
See: bat --help (-r)
Page the output
bat --paging=never hello.py
bat -P hello.py
bat --pager='less -R' hello.py
bat pages only when the output exceeds one screen. Piped output never pages. -P is short for --paging=never.
See: Automatic paging
Show Git changes
bat -d notes.txt
bat -d --diff-context=1 notes.txt
Output
───────┬────────────────────────────────
│ File: notes.txt
───────┼────────────────────────────────
1 │ one
2 ~ │ TWO
3 │ three
4 + │ four
───────┴────────────────────────────────
~ marks modified lines and + marks added ones. --diff-context controls the surrounding context.
See: Git integration
Work with other tools
Preview in fzf
fzf --preview "bat --color=always \
--style=numbers --line-range=:500 {}"
Force colors for previews; --line-range keeps long files fast to load.
Follow a log
tail -f app.log | bat --paging=never \
-l log
Disable paging for a live stream, and name the language when a path cannot provide it.
View a Git revision
git show HEAD:src/main.py | bat -l py
Older file versions have no path on disk, so set the language explicitly.
Colorize man pages
export MANPAGER="bat -plman"
man 2 select
-plman combines plain style with the manpage syntax.
Configure
Config file
bat --config-file
bat --generate-config-file
bat cache --build
The config file holds default flags, one per line. Rebuild the cache after adding themes or syntaxes.
See: Configuration file
Environment variables
| Variable | Effect |
|---|---|
BAT_THEME | Default theme |
BAT_STYLE | Default style components |
BAT_PAGER | Pager command |
BAT_PAGING | auto, never, always |
BAT_CONFIG_PATH | Config file location |
Flags take precedence over environment variables, which take precedence over the config file.
See: Customization
0 Comments for this cheatsheet. Write yours!