Repository: sharkdp/bat
Stars: 58256
README.md
<p align="center">
<img src="doc/logo-header.svg" alt="bat - a cat clone with wings"><br>
<a href="https://github.com/sharkdp/bat/actions?query=workflow%3ACICD"><img src="https://github.com/sharkdp/bat/workflows/CICD/badge.svg" alt="Build Status"></a>
<img src="https://img.shields.io/crates/l/bat.svg" alt="license">
<a href="https://crates.io/crates/bat"><img src="https://img.shields.io/crates/v/bat.svg?colorB=319e8c" alt="Version info"></a><br>
A <i>cat(1)</i> clone with syntax highlighting and Git integration.
</p>
<p align="center">
<a href="#syntax-highlighting">Key Features</a> •
<a href="#how-to-use">How To Use</a> •
<a href="#installation">Installation</a> •
<a href="#customization">Customization</a> •
<a href="#project-goals-and-alternatives">Project goals, alternatives</a><br>
[English]
[<a href="doc/README-zh.md">中文</a>]
[<a href="doc/README-ja.md">日本語</a>]
[<a href="doc/README-ko.md">한국어</a>]
[<a href="doc/README-ru.md">Русский</a>]
</p>
Sponsors
A special thank you goes to our biggest <a href="doc/sponsors.md">sponsors</a>:<br>
<p>
<a href="https://www.warp.dev/bat">
<img src="doc/sponsors/warp-logo.png" width="200" alt="Warp">
<br>
<strong>Warp, the intelligent terminal</strong>
<br>
<sub>Available on MacOS, Linux, Windows</sub>
</a>
</p>
Syntax highlighting
bat supports syntax highlighting for a large number of programming and markup
languages:
Git integration
bat communicates with git to show modifications with respect to the index
(see left sidebar):
Show non-printable characters
You can use the -A/--show-all option to show and highlight non-printable
characters:
!Non-printable character example
Automatic paging
By default, bat pipes its own output to a pager (e.g. less) if the output is too large for one screen.
If you would rather bat work like cat all the time (never page output), you can set --paging=never as an option, either on the command line or in your configuration file.
If you intend to alias cat to bat in your shell configuration, you can use alias cat='bat --paging=never' to preserve the default behavior.
#### File concatenation
Even with a pager set, you can still use bat to concatenate files :wink:.
Whenever bat detects a non-interactive terminal (i.e. when you pipe into another process or into a file), bat will act as a drop-in replacement for cat and fall back to printing the plain file contents, regardless of the --pager option's value.
How to use
Display a single file on the terminal
bat README.mdDisplay multiple files at once
bat src/*.rsRead from stdin, determine the syntax automatically (note, highlighting will
only work if the syntax can be determined from the first line of the file,
usually through a shebang such as #!/bin/sh)
curl -s https://sh.rustup.rs | batRead from stdin, specify the language explicitly
yaml2json .travis.yml | json_pp | bat -l jsonShow and highlight non-printable characters:
bat -A /etc/hostsUse it as a cat replacement:
bat > note.md # quickly create a new filebat header.md content.md footer.md > document.md
bat -n main.rs # show line numbers (only)
bat f - g # output 'f', then stdin, then 'g'.
Integration with other tools
#### fzf
You can use bat as a previewer for fzf. To do this,
use bat's --color=always option to force colorized output. You can also use --line-range
option to restrict the load times for long files:
fzf --preview "bat --color=always --style=numbers --line-range=:500 {}"For more information, see fzf's README.
#### find or fd
You can use the -exec option of find to preview all search results with bat:
find … -exec bat {} +If you happen to use fd, you can use the -X/--exec-batch option to do the same:
fd … -X bat#### ripgrep
With batgrep, bat can be used as the printer for ripgrep search results.
batgrep needle src/#### tail -f
bat can be combined with tail -f to continuously monitor a given file with syntax highlighting.
tail -f /var/log/pacman.log | bat --paging=never -l logNote that we have to switch off paging in order for this to work. We have also specified the syntax
explicitly (-l log), as it can not be auto-detected in this case.
#### git
You can combine bat with git show to view an older version of a given file with proper syntax
highlighting:
git show v0.6.0:src/main.rs | bat -l rs#### git diff
You can combine bat with git diff to view lines around code changes with proper syntax
highlighting:
batdiff() {
git diff --name-only --relative --diff-filter=d -z | xargs -0 bat --diff
}If you prefer to use this as a separate tool, check out
batdiff in bat-extras.If you are looking for more support for git and diff operations, check out delta.
#### xclip
The line numbers and Git modification markers in the output of bat can make it hard to copy
the contents of a file. To prevent this, you can call bat with the -p/--plain option or
simply pipe the output into xclip:
bat main.cpp | xclipbat will detect that the output is being redirected and print the plain file contents.#### man
bat can be used as a colorizing pager for man, by setting theMANPAGER environment variable:
export MANPAGER="bat -plman"
man 2 select(on some older Debian or Ubuntu releases, the executable is named
batcat instead of bat)If you prefer to have this bundled in a new command, you can also use batman.
Note that the Manpage syntax is developed in this repository and still needs some work.
#### prettier / shfmt / rustfmt
The prettybat script is a wrapper that will format code and print it with bat.
#### Warp
<a href="https://app.warp.dev/drive/folder/-Bat-Warp-Pack-lxhe7HrEwgwpG17mvrFSz1">
<img src="doc/sponsors/warp-pack-header.png" alt="Warp">
</a>
#### Highlighting --help messages
You can use bat to colorize help text: $ cp --help | bat -plhelp
You can also use a wrapper around this:
in your .bashrc/.zshrc/*rc
alias bathelp='bat --plain --language=help'
help() {
"$@" --help 2>&1 | bathelp
}Then you can do $ help cp or $ help git commit.
When you are using zsh, you can also use global aliases to override -h and --help entirely:
alias -g -- -h='-h 2>&1 | bat --language=help --style=plain'
alias -g -- --help='--help 2>&1 | bat --language=help --style=plain'For fish, you can use abbreviations:
abbr -a --position anywhere -- --help '--help | bat -plhelp'
abbr -a --position anywhere -- -h '-h | bat -plhelp'This way, you can keep on using cp --help, but get colorized help pages.
To remove these abbreviations later, run:
``fishabbr -e -- --help
abbr -e -- -h
`The --before the abbreviation name is required because--helpand-hstart with dashes, which would otherwise be interpreted as flags toabbritself.
Be aware that in some cases, -h may not be a shorthand of --help (for example with ls). In cases where you need to use -h \
as a command argument you can prepend to the argument (eg. ls \-h) to escape the aliasing defined above.
Please report any issues with the help syntax in this repository.
Installation
<!--
Installation instructions need to:
* be for widely used systems
* be non-obvious
* be from somewhat official sources
-->

On Ubuntu (using apt)
... and other Debian-based Linux distributions.
bat is available on Ubuntu since 20.04 ("Focal") and Debian since August 2021 (Debian 11 - "Bullseye").
If your Ubuntu/Debian installation is new enough you can simply run:
sudo apt install batImportant: On some older Ubuntu/Debian releases, the executable is installed as batcat If the package has not yet been promoted to your Ubuntu/Debian installation, or you want You can install the You can install the You can install the You can install the You can install You can install There is currently no recommended snap package available. You can install Or install There are a few options to install #### Prerequisites You will need to install the Visual C++ Redistributable #### With WinGet You can install You can install You can install You can download prebuilt binaries from the Release page, You will need to install the Visual C++ Redistributable package. Check out the Release page for If you want to build #### From local source The --path . #### From Furthermore, shell completions are also available by running: Use If you want to preview the different themes on a custom file, you can use You can also use a custom theme by following the - Although these themes are more restricted, they have three advantages over truecolor themes. They: - Enjoy maximum compatibility. Some terminal utilities do not support more than 3-bit colors. You can use the >[!tip] By default, There is also the Should you find that a particular syntax is not available within A good resource for finding Sublime Syntax packages is Package Control. Once you found a 1. Create a folder with syntax definition files: instead of bat (due to a namebat
clash with another package). On newer releases, the executable is available as . If bat --version does not work after installation, try batcat --version instead. You can set up a bat -> batcat symlink or alias to prevent any issues that may come up because of this and to be consistent with other distributions:` bash
mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/batan example alias for batcat as bat:
alias bat="batcat".debOn Ubuntu (using most recent
packages) bat
... and other Debian-based Linux distributions.
the most recent release of , download the latest .deb package from the
release page and install it via:
sudo dpkg -i bat_0.18.3_amd64.deb # adapt version number and architecturebatOn Alpine Linux
package
from the official sources, provided you have the appropriate repository enabled:
apk add batbatOn Arch Linux
package
from the official sources:
pacman -S batbatOn Fedora
package from the official Fedora Modular repository.
dnf install batbatOn Gentoo Linux
package
from the official sources:
emerge sys-apps/bat
bash
pkg install bator build it on your own from the FreeBSD ports:
cd /usr/ports/textproc/bat
make install
bash
pkg_add batbatVia nix
using the nix package manager:
nix-env -i batbatOn openSUSE
with zypper:
zypper install batbatVia snap package
Existing packages may be available, but are not officially supported and may contain issues.On macOS (or Linux) via Homebrew
with Homebrew:
brew install batbatOn macOS via MacPorts
with MacPorts:
port install batbatOn Windows
on Windows. Once you have installed bat,bat
take a look at the "Using on Windows" section.bat via WinGet:
winget install sharkdp.bat#### With Chocolateybat via Chocolatey:
choco install bat#### With Scoopbat via scoop:
scoop install bat#### From prebuilt binaries:batFrom binaries
prebuilt versions of for many different architectures. Statically-linkedmusl
binaries are also available: look for archives with in the file name.batFrom source
from source, you need Rust 1.79.0 orcargo
higher. You can then use to build everything:
cargo install --path . --lockedcargo install above specifies the directory of the source code and NOT where bat will be installed.For more information see the docs for
.crates.io
cargo install --locked batNote that additional files like the man page or shell completioncargo
files can not be installed automatically in both these ways.
If installing from a local source, they will be generated by build
and should be available in the cargo target folder under .
bat --completion <shell>see --help for supported shells
bat --list-themesCustomization
Highlighting theme
to get a list of all available themes for syntaxbat
highlighting. By default, uses Monokai Extended or Monokai Extended LightTwoDark
for dark and light themes respectively. To select the theme, call bat--theme=TwoDark
with the option or set the BAT_THEME environment variable toTwoDark. Use export BAT_THEME="TwoDark" in your shell's startup file tobat
make the change permanent. Alternatively, use 'sfzf
configuration file.
the following command (you need for this):
bat --list-themes | fzf --preview="bat --theme={} --color=always /path/to/file"bat automatically picks a fitting theme depending on your terminal's background color.--theme-dark
You can use the / --theme-light options or the BAT_THEME_DARK / BAT_THEME_LIGHT environment variablesbat
to customize the themes used. This is especially useful if you frequently switch between dark and light mode.
'Adding new themes' section below.8-bit themes
has three themes that always use 8-bit colors,ansi
even when truecolor support is available: looks decent on any terminal. It uses 3-bit colors: black, red, green,base16
yellow, blue, magenta, cyan, and white.
- is designed for base16 terminal themes. It usesbase16-256
4-bit colors (3-bit colors plus bright variants) in accordance with the
base16 styling guidelines.
- is designed for tinted-shell.--style
It replaces certain bright colors with 8-bit colors from 16 to 21. Do not use this simply
because you have a 256-color terminal but are not using tinted-shell.
- Adapt to terminal theme changes. Even for already printed output.
- Visually harmonize better with other terminal software.Output style
option to control the appearance of bat's output.--style=numbers,changes
You can use , for example, to show only Git changesBAT_STYLE
and line numbers but no grid and no file header. Set the environmentbat
variable to make these changes permanent or use 'sbat
configuration file.If you specify a default style in
's config file, you can change which componentsbatare displayed during a single run of
using the --style command-line argument.+By prefixing a component with
or -, it can be added or removed from the current style.--style=full,-snip> For example, if your config contains
, you can run bat with--style=-grid,+snip to remove the grid and add back the snip component.--style=numbersOr, if you want to override the styles completely, you use
tobatonly show the line numbers.
Decorations
only shows decorations (such as line numbers, file headers, grid borders, etc.) when outputting to an interactive terminal. You can control this behavior with the --decorations option. Use --decorations=always to show decorations even when piping output to another command, or --decorations=never to disable them entirely. Possible values are auto (default), never, and always.--force-colorization option, which is an alias for --decorations=always --color=always. This is useful if you want to keep colorization and decorations when piping bat's output to another program.batAdding new syntaxes / language definitions
, you can follow thesebat
instructions to easily add new syntaxes to your current installation.bat uses the excellent syntectsyntect
library for syntax highlighting. can read any.sublime-syntax
Sublime Text file
and theme.
syntax:
mkdir -p "$(bat --config-dir)/syntaxes"
cd "$(bat --config-dir)/syntaxes"
# Put new '.sublime-syntax' language definition files
# in this folder (or its subdirectories), for example:
git clone https://github.com/tellnobody1/sublime-purescript-syntax
2. Now use the following command to parse these files into a binary cache:bat cache --build
3. Finally, usebat --list-languagesto check if the new languages are available.If you ever want to go back to the default settings, call:
bat cache --clear
4. If you think that a specific syntax should be included inbatby default, please.sublime-color-scheme
consider opening a "syntax request" ticket after reading the policies and
instructions here: Open Syntax Request.Adding new themes
This works very similar to how we add new syntax definitions.
NOTECustom themes must be stored in .tmTheme
files.Newerfiles are currently not supported.First, create a folder with the new syntax highlighting themes:
mkdir -p "$(bat --config-dir)/themes"
cd "$(bat --config-dir)/themes"
Download a theme in '.tmTheme' format, for example:
git clone https://github.com/greggb/sublime-snazzy
Update the binary cache
bat cache --build
Finally, usebat --list-themesto check if the new themes are available.--map-syntaxNOTEbat
uses the name of the.tmThemefile for the theme's name.Adding or changing file type associations
You can add new (or change existing) file name patterns using the
pattern:syntax
command line option. The option takes an argument of the formwherepatternis a glob pattern that is matched against the file name andsyntax
the absolute file path. Thepart is the full name of a supported languagebat --list-languages
(usefor an overview).batNote: You probably want to use this option as an entry in
's configuration file-l
for persistence instead of passing it on the command line as a one-off. Generally
you'd just useif you want to manually specify a language for a file..confExample: To use "INI" syntax highlighting for all files with a
file extension, use
--map-syntax='*.conf:INI'
Example: To open all files called.ignore(exact match) with the "Git Ignore" syntax, use:
--map-syntax='.ignore:Git Ignore'
Example: To open all.conffiles in subfolders of/etc/apache2with the "Apache Conf"
syntax, use (this mapping is already built in):
--map-syntax='/etc/apache2//*.conf:Apache Conf'
batUsing a different pager
uses the pager that is specified in thePAGERenvironment variable. If this variable is notless
set,is used by default. You can also use bat's built-in pager with--pager=builtinorBAT_PAGER
by setting theenvironment variable to "builtin".PAGERIf you want to use a different pager, you can either modify the
variable or set theBAT_PAGERenvironment variable to override what is specified inPAGER.PAGER>[!NOTE]
Ifismoreormost,batwill silently uselessinstead to ensure support for colors.PAGERIf you want to pass command-line arguments to the pager, you can also set them via the
/BAT_PAGERvariables:
export BAT_PAGER="less -RFK"
Instead of using environment variables, you can also usebat's configuration file to configure the pager (--pageroption).less
Using
as a pagerlessWhen using
as a pager,batwill automatically pass extra options along toless-R
to improve the experience. Specifically,/--RAW-CONTROL-CHARS,-F/--quit-if-one-screen,-K/--quit-on-intrand under certain conditions,-X/--no-initand/or-S/--chop-long-lines.less>[!IMPORTANT]
These options will not be added if:- The pager is not named.--pager- Theargument contains any command-line arguments (e.g.--pager="less -R").BAT_PAGER- Theenvironment variable contains any command-line arguments (e.g.export BAT_PAGER="less -R")--quit-if-one-screen> Theoption will not be added when:--paging=always- Theargument is used.BAT_PAGING- Theenvironment is set toalways.-RThe
/--RAW-CONTROL-CHARSoption is needed to interpret ANSI colors correctly.-FThe
/--quit-if-one-screenoption instructslessto exit immediately if the output size is smaller thanq
the vertical size of the terminal. This is convenient for small files because you do not
have to pressto quit the pager.-KThe
/--quit-on-introption instructslessto exit immediately when an interrupt signal is received.less
This is useful to ensure thatquits together withbaton SIGINT.-XThe
/--no-initoption is added to versions oflessolder than version 530 (older than 558 on Windows) to-F
fix a bug with the/--quit-if-one-screenfeature. Unfortunately, it also breaks mouse-wheel support inless.less
If you want to enable mouse-wheel scrolling on older versions ofand do not mind losing--pager
the quit-if-one-screen feature, you can set the pager (viaorBAT_PAGER) toless -R.less
For530 or newer, it should work out of the box.-SThe
/--chop-long-linesoption is added whenbat's-S/--chop-long-linesoption is used. This tellslessbat
to truncate any lines larger than the terminal width.Indentation
expands tabs to 4 spaces by itself, not relying on the pager. To change this, simply add the--tabsargument with the number of spaces you want to be displayed.--pagerNote: Defining tab stops for the pager (via the
argument bybat, or via theLESSless
environment variable for) won't be taken into account because the pager will already getbat
expanded spaces instead of tabs. This behaviour is added to avoid indentation issues caused by the
sidebar. Callingwith--tabs=0will override it and let tabs be consumed by the pager.batDark mode
If you make use of the dark mode feature in macOS, you might want to configure
to use a differentdefault
theme based on the OS theme. The following snippet uses thetheme when in the _dark mode_GitHub
and thetheme when in the _light mode_.
alias cat="bat --theme auto:system --theme-dark default --theme-light GitHub"
The same dark mode feature is now available in GNOME and affects theorg.gnome.desktop.interface color-schemesetting. The following code converts the above to use said setting.
.bashrc
sys_color_scheme_is_dark() {
condition=$(gsettings get org.gnome.desktop.interface color-scheme)
condition=$(echo "$condition" | tr -d "[:space:]'")
if [ $condition == "prefer-dark" ]; then
return 0
else
return 1
fi
}
bat_alias_wrapper() {
#get color scheme
sys_color_scheme_is_dark
if [[ $? -eq 0 ]]; then
# bat command with dark color scheme
bat --theme=default "$@"
else
# bat command with light color scheme
bat --theme=GitHub "$@"
fi
}
alias cat='bat_alias_wrapper'
batConfiguration file
can also be customized with a configuration file. The location of the file is dependent
on your operating system. To get the default path for your system, call
bat --config-file
Alternatively, you can useBAT_CONFIG_PATHorBAT_CONFIG_DIRenvironment variables to pointbat
to a non-default location of the configuration file or the configuration directory respectively:
export BAT_CONFIG_PATH="/path/to/bat/bat.conf"
export BAT_CONFIG_DIR="/path/to/bat"
A default configuration file can be created with the--generate-config-fileoption.
bat --generate-config-file
There is also now a systemwide configuration file, which is located under/etc/bat/configonC:\ProgramData\bat\config
Linux and Mac OS andon windows. If the system wide configurationbat --help
file is present, the content of the user configuration will simply be appended to it.Format
The configuration file is a simple list of command line arguments. Use
to see a full list of possible options and values. In addition, you can add comments by prepending a line with the#character.Example configuration file:
Set the theme to "TwoDark"
--theme="TwoDark"
Show line numbers, Git modifications and file header (but no grid)
--style="numbers,changes,header"
Use italic text on the terminal (not supported on all terminals)
--italic-text=always
Use C++ syntax for Arduino .ino files
--map-syntax "*.ino:C++"
batUsing
on Windowsbatmostly works out-of-the-box on Windows, but a few features may need extra configuration.morePrerequisites
You will need to install the Visual C++ Redistributable package.
Paging
Windows only includes a very limited pager in the form of
. You can download a Windows binaryless
forfrom its homepage or throughPATH
Chocolatey. To use it, place the binary in a directory in
youror define an environment variable. The Chocolatey package installslessautomatically.conhost.exeColors
Windows 10 natively supports colors in both
(Command Prompt) and PowerShell sinceless
v1511), as
well as in newer versions of bash. On earlier versions of Windows, you can use
Cmder, which includes ConEmu.Note: Old versions of
do not correctly interpret colors on Windows. To fix this, you can add the optional Unix tools to your PATH when installing Git. If you don’t have any other pagers installed, you can disable paging entirely by passing--paging=neveror by settingBAT_PAGERto an empty string.batCygwin
on Windows does not natively support Cygwin's unix-style paths (/cygdrive/*). When passed an absolute cygwin path as an argument,batwill encounter the following error:The system cannot find the path specified. (os error 3).bash_profileThis can be solved by creating a wrapper or adding the following function to your
file:
bat() {
local index
local args=("$@")
for index in $(seq 0 ${#args[@]}) ; do
case "${args[index]}" in
-*) continue;;
*) [ -e "${args[index]}" ] && args[index]="$(cygpath --windows "${args[index]}")";;
esac
done
command bat "${args[@]}"
}
batTroubleshooting
Garbled output
If an input file contains color codes or other ANSI escape sequences or control characters,
will have problemsbat
performing syntax highlighting and text wrapping, and thus the output can become garbled.If your version of
supports the--strip-ansi=autooption, it can be used to remove such sequences--color=never --wrap=never
before syntax highlighting. Alternatively, you may disable both syntax highlighting and wrapping by
passing theoptions tobat.batNOTEThe auto
option of--strip-ansiavoids removing escape sequences when the syntax is plain text.Terminals & colors
handles terminals with and without truecolor support. However, the colors in most syntaxterminator
highlighting themes are not optimized for 8-bit colors. It is therefore strongly recommended
that you use a terminal with 24-bit truecolor support (,konsole,iTerm2, ...),COLORTERM
or use one of the basic 8-bit themes designed for a restricted set of colors.
See this article for more details and a full list of
terminals with truecolor support.Make sure that your truecolor terminal sets the
variable to eithertruecoloror24bit. Otherwise,batwill not be able to determine whether or not 24-bit escape sequencesbat --list-themes
are supported (and fall back to 8-bit colors).Line numbers and grid are hardly visible
Please try a different theme (see
for a list). TheOneHalfDarkandOneHalfLightthemes provide grid and line colors that are brighter.batFile encodings
natively supports UTF-8 as well as UTF-16. For every other file encoding, you may need toiconv
convert to UTF-8 first because the encodings can typically not be auto-detected. You can
to do so.
Example: if you have a PHP file in Latin-1 (ISO-8859-1) encoding, you can call:
iconv -f ISO-8859-1 -t UTF-8 my-file.php | bat
Note: you might have to use the-l/--languageoption if the syntax can not be auto-detectedbat
by.Development
Recursive clone to retrieve all submodules
git clone --recursive https://github.com/sharkdp/bat
Build (debug version)
cd bat
cargo build --bins
Run unit tests and integration tests
cargo test
Install (release version)
cargo install --path . --locked
Build a bat binary with modified syntaxes and themes
bash assets/create.sh
cargo install --path . --locked --force
`
If you want to build an application that uses
bat's pretty-printing
features as a library, check out the the API documentation.
Note that you have to use either regex-onig or regex-fancy as a feature
when you depend on bat as a library.Contributing
CONTRIBUTING.md guide.Maintainers
- sharkdp
- eth-p
- keith-hall
- Enselic
Security vulnerabilities
SECURITY.md.Project goals and alternatives
bat tries to achieve the following goals:- Provide beautiful, advanced syntax highlighting
- Integrate with Git to show file modifications
- Be a drop-in replacement for (POSIX)
cat
- Offer a user-friendly command-line interfaceThere are a lot of alternatives, if you are looking for similar programs. See
this document for a comparison.
License
Copyright (c) 2018-2025 bat-developers.bat` is made available under the terms of either the MIT License or the Apache License 2.0, at your option.See the LICENSE-APACHE and LICENSE-MIT files for license details.