Docs: clarify PATH requirement and explain highlighted outputs (Fixes… (#3610)
Docs: clarify PATH requirement and explain highlighted outputs (Fixes #2376)
Update CHANGELOG for PR #3610
Apply suggestion from @keith-hall
Applied reviewer’s suggestion to improve PATH export: safer with quotes and more convenient with $(pwd).
Co-authored-by: Keith Hall keith-hall@users.noreply.github.com
Docs: clarify PATH setup in CONTRIBUTING.md, remove that section from README.md
Docs: add cross reference to syntax tests step 5 in doc/assets.md
Co-authored-by: Keith Hall keith-hall@users.noreply.github.com
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802032778号
A cat(1) clone with syntax highlighting and Git integration.
Key Features • How To Use • Installation • Customization • Project goals, alternatives
[English] [中文] [日本語] [한국어] [Русский]
Sponsors
A special thank you goes to our biggest sponsors:
Warp, the intelligent terminal
Available on MacOS, Linux, Windows
Syntax highlighting
batsupports syntax highlighting for a large number of programming and markup languages:Git integration
batcommunicates withgitto show modifications with respect to the index (see left side bar):Show non-printable characters
You can use the
-A/--show-alloption to show and highlight non-printable characters:Automatic paging
By default,
batpipes its own output to a pager (e.g.less) if the output is too large for one screen. If you would ratherbatwork likecatall the time (never page output), you can set--paging=neveras an option, either on the command line or in your configuration file. If you intend to aliascattobatin your shell configuration, you can usealias cat='bat --paging=never'to preserve the default behavior.File concatenation
Even with a pager set, you can still use
.
Whenever
batto concatenate filesbatdetects a non-interactive terminal (i.e. when you pipe into another process or into a file),batwill act as a drop-in replacement forcatand fall back to printing the plain file contents, regardless of the--pageroption’s value.How to use
Display a single file on the terminal
Display multiple files at once
Read 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)Read from stdin, specify the language explicitly
Show and highlight non-printable characters:
Use it as a
catreplacement:Integration with other tools
fzfYou can use
batas a previewer forfzf. To do this, usebat‘s--color=alwaysoption to force colorized output. You can also use--line-rangeoption to restrict the load times for long files:For more information, see
fzf‘sREADME.findorfdYou can use the
-execoption offindto preview all search results withbat:If you happen to use
fd, you can use the-X/--exec-batchoption to do the same:ripgrepWith
batgrep,batcan be used as the printer forripgrepsearch results.tail -fbatcan be combined withtail -fto continuously monitor a given file with syntax highlighting.Note 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.gitYou can combine
batwithgit showto view an older version of a given file with proper syntax highlighting:git diffYou can combine
batwithgit diffto view lines around code changes with proper syntax highlighting:If you prefer to use this as a separate tool, check out
batdiffinbat-extras.If you are looking for more support for git and diff operations, check out
delta.xclipThe line numbers and Git modification markers in the output of
batcan make it hard to copy the contents of a file. To prevent this, you can callbatwith the-p/--plainoption or simply pipe the output intoxclip:batwill detect that the output is being redirected and print the plain file contents.manbatcan be used as a colorizing pager forman, by setting theMANPAGERenvironment variable:(replace
batwithbatcatif you are on Debian or Ubuntu)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/rustfmtThe
prettybatscript is a wrapper that will format code and print it withbat.WarpHighlighting
--helpmessagesYou can use
batto colorize help text:$ cp --help | bat -plhelpYou can also use a wrapper around this:
Then you can do
$ help cpor$ help git commit.When you are using
zsh, you can also use global aliases to override-hand--helpentirely:For
fish, you can use abbreviations:This way, you can keep on using
cp --help, but get colorized help pages.Be aware that in some cases,
-hmay not be a shorthand of--help(for example withls). In cases where you need to use-has 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
On Ubuntu (using
apt)… and other Debian-based Linux distributions.
batis 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:
Important: If you install
batthis way, please note that the executable may be installed asbatcatinstead ofbat(due to a name clash with another package). You can set up abat -> batcatsymlink or alias to prevent any issues that may come up because of this and to be consistent with other distributions:an example alias for
batcatasbat:On Ubuntu (using most recent
.debpackages)… and other Debian-based Linux distributions.
If the package has not yet been promoted to your Ubuntu/Debian installation, or you want the most recent release of
bat, download the latest.debpackage from the release page and install it via:On Alpine Linux
You can install the
batpackage from the official sources, provided you have the appropriate repository enabled:On Arch Linux
You can install the
batpackage from the official sources:On Fedora
You can install the
batpackage from the official Fedora Modular repository.On Gentoo Linux
You can install the
batpackage from the official sources:On FreeBSD
You can install a precompiled
batpackage with pkg:or build it on your own from the FreeBSD ports:
On OpenBSD
You can install
batpackage usingpkg_add(1):Via nix
You can install
batusing the nix package manager:On openSUSE
You can install
batwith zypper:Via snap package
There is currently no recommended snap package available. Existing packages may be available, but are not officially supported and may contain issues.
On macOS (or Linux) via Homebrew
You can install
batwith Homebrew:On macOS via MacPorts
Or install
batwith MacPorts:On Windows
There are a few options to install
baton Windows. Once you have installedbat, take a look at the “Usingbaton Windows” section.Prerequisites
You will need to install the Visual C++ Redistributable
With WinGet
You can install
batvia WinGet:With Chocolatey
You can install
batvia Chocolatey:With Scoop
You can install
batvia scoop:From prebuilt binaries:
You can download prebuilt binaries from the Release page,
You will need to install the Visual C++ Redistributable package.
From binaries
Check out the Release page for prebuilt versions of
batfor many different architectures. Statically-linked binaries are also available: look for archives withmuslin the file name.From source
If you want to build
batfrom source, you need Rust 1.79.0 or higher. You can then usecargoto build everything:From local source
From
crates.ioNote that additional files like the man page or shell completion files can not be installed automatically in both these ways. If installing from a local source, they will be generated by
cargoand should be available in the cargo target folder underbuild.Furthermore, shell completions are also available by running:
Customization
Highlighting theme
Use
bat --list-themesto get a list of all available themes for syntax highlighting. By default,batusesMonokai ExtendedorMonokai Extended Lightfor dark and light themes respectively. To select theTwoDarktheme, callbatwith the--theme=TwoDarkoption or set theBAT_THEMEenvironment variable toTwoDark. Useexport BAT_THEME="TwoDark"in your shell’s startup file to make the change permanent. Alternatively, usebat‘s configuration file.If you want to preview the different themes on a custom file, you can use the following command (you need
fzffor this):batautomatically picks a fitting theme depending on your terminal’s background color. You can use the--theme-dark/--theme-lightoptions or theBAT_THEME_DARK/BAT_THEME_LIGHTenvironment variables to customize the themes used. This is especially useful if you frequently switch between dark and light mode.You can also use a custom theme by following the ‘Adding new themes’ section below.
8-bit themes
bathas three themes that always use 8-bit colors, even when truecolor support is available:ansilooks decent on any terminal. It uses 3-bit colors: black, red, green, yellow, blue, magenta, cyan, and white.base16is designed for base16 terminal themes. It uses 4-bit colors (3-bit colors plus bright variants) in accordance with the base16 styling guidelines.base16-256is designed for tinted-shell. 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.Although these themes are more restricted, they have three advantages over truecolor themes. They:
Output style
You can use the
--styleoption to control the appearance ofbat‘s output. You can use--style=numbers,changes, for example, to show only Git changes and line numbers but no grid and no file header. Set theBAT_STYLEenvironment variable to make these changes permanent or usebat‘s configuration file.Decorations
By default,
batonly shows decorations (such as line numbers, file headers, grid borders, etc.) when outputting to an interactive terminal. You can control this behavior with the--decorationsoption. Use--decorations=alwaysto show decorations even when piping output to another command, or--decorations=neverto disable them entirely. Possible values areauto(default),never, andalways.There is also the
--force-colorizationoption, which is an alias for--decorations=always --color=always. This is useful if you want to keep colorization and decorations when pipingbat‘s output to another program.Adding new syntaxes / language definitions
Should you find that a particular syntax is not available within
bat, you can follow these instructions to easily add new syntaxes to your currentbatinstallation.batuses the excellentsyntectlibrary for syntax highlighting.syntectcan read any Sublime Text.sublime-syntaxfile and theme.A good resource for finding Sublime Syntax packages is Package Control. Once you found a syntax:
Create a folder with syntax definition files:
Now use the following command to parse these files into a binary cache:
Finally, use
bat --list-languagesto check if the new languages are available.If you ever want to go back to the default settings, call:
If you think that a specific syntax should be included in
batby default, please 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.
First, create a folder with the new syntax highlighting themes:
Finally, use
bat --list-themesto check if the new themes are available.Adding or changing file type associations
You can add new (or change existing) file name patterns using the
--map-syntaxcommand line option. The option takes an argument of the formpattern:syntaxwherepatternis a glob pattern that is matched against the file name and the absolute file path. Thesyntaxpart is the full name of a supported language (usebat --list-languagesfor an overview).Note: You probably want to use this option as an entry in
bat‘s configuration file for persistence instead of passing it on the command line as a one-off. Generally you’d just use-lif you want to manually specify a language for a file.Example: To use “INI” syntax highlighting for all files with a
.conffile extension, useExample: To open all files called
.ignore(exact match) with the “Git Ignore” syntax, use:Example: To open all
.conffiles in subfolders of/etc/apache2with the “Apache Conf” syntax, use (this mapping is already built in):Using a different pager
batuses the pager that is specified in thePAGERenvironment variable. If this variable is not set,lessis used by default. You can also use bat’s built-in pager with--pager=builtinor by setting theBAT_PAGERenvironment variable to “builtin”.If you want to use a different pager, you can either modify the
PAGERvariable or set theBAT_PAGERenvironment variable to override what is specified inPAGER.If you want to pass command-line arguments to the pager, you can also set them via the
PAGER/BAT_PAGERvariables:Instead of using environment variables, you can also use
bat‘s configuration file to configure the pager (--pageroption).Using
lessas a pagerWhen using
lessas a pager,batwill automatically pass extra options along tolessto improve the experience. Specifically,-R/--RAW-CONTROL-CHARS,-F/--quit-if-one-screen,-K/--quit-on-intrand under certain conditions,-X/--no-initand/or-S/--chop-long-lines.The
-R/--RAW-CONTROL-CHARSoption is needed to interpret ANSI colors correctly.The
-F/--quit-if-one-screenoption instructslessto exit immediately if the output size is smaller than the vertical size of the terminal. This is convenient for small files because you do not have to pressqto quit the pager.The
-K/--quit-on-introption instructslessto exit immediately when an interrupt signal is received. This is useful to ensure thatlessquits together withbaton SIGINT.The
-X/--no-initoption is added to versions oflessolder than version 530 (older than 558 on Windows) to fix a bug with the-F/--quit-if-one-screenfeature. Unfortunately, it also breaks mouse-wheel support inless. If you want to enable mouse-wheel scrolling on older versions oflessand do not mind losing the quit-if-one-screen feature, you can set the pager (via--pagerorBAT_PAGER) toless -R. Forless530 or newer, it should work out of the box.The
-S/--chop-long-linesoption is added whenbat‘s-S/--chop-long-linesoption is used. This tellslessto truncate any lines larger than the terminal width.Indentation
batexpands 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.Note: Defining tab stops for the pager (via the
--pagerargument bybat, or via theLESSenvironment variable forless) won’t be taken into account because the pager will already get expanded spaces instead of tabs. This behaviour is added to avoid indentation issues caused by the sidebar. Callingbatwith--tabs=0will override it and let tabs be consumed by the pager.Dark mode
If you make use of the dark mode feature in macOS, you might want to configure
batto use a different theme based on the OS theme. The following snippet uses thedefaulttheme when in the dark mode and theGitHubtheme when in the light mode.The same dark mode feature is now available in GNOME and affects the
org.gnome.desktop.interface color-schemesetting. The following code converts the above to use said setting.Configuration file
batcan 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, callAlternatively, you can use
BAT_CONFIG_PATHorBAT_CONFIG_DIRenvironment variables to pointbatto a non-default location of the configuration file or the configuration directory respectively:A default configuration file can be created with the
--generate-config-fileoption.There is also now a systemwide configuration file, which is located under
/etc/bat/configon Linux and Mac OS andC:\ProgramData\bat\configon windows. If the system wide configuration 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
bat --helpto 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:
Using
baton Windowsbatmostly works out-of-the-box on Windows, but a few features may need extra configuration.Prerequisites
You will need to install the Visual C++ Redistributable package.
Paging
Windows only includes a very limited pager in the form of
more. You can download a Windows binary forlessfrom its homepage or through Chocolatey. To use it, place the binary in a directory in yourPATHor define an environment variable. The Chocolatey package installslessautomatically.Colors
Windows 10 natively supports colors in both
conhost.exe(Command Prompt) and PowerShell since 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
lessdo 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.Cygwin
baton 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)This can be solved by creating a wrapper or adding the following function to your
.bash_profilefile:Troubleshooting
Garbled output
If an input file contains color codes or other ANSI escape sequences or control characters,
batwill have problems performing syntax highlighting and text wrapping, and thus the output can become garbled.If your version of
batsupports the--strip-ansi=autooption, it can be used to remove such sequences before syntax highlighting. Alternatively, you may disable both syntax highlighting and wrapping by passing the--color=never --wrap=neveroptions tobat.Terminals & colors
bathandles terminals with and without truecolor support. However, the colors in most syntax highlighting themes are not optimized for 8-bit colors. It is therefore strongly recommended that you use a terminal with 24-bit truecolor support (terminator,konsole,iTerm2, …), 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
COLORTERMvariable to eithertruecoloror24bit. Otherwise,batwill not be able to determine whether or not 24-bit escape sequences are supported (and fall back to 8-bit colors).Line numbers and grid are hardly visible
Please try a different theme (see
bat --list-themesfor a list). TheOneHalfDarkandOneHalfLightthemes provide grid and line colors that are brighter.File encodings
batnatively supports UTF-8 as well as UTF-16. For every other file encoding, you may need to convert to UTF-8 first because the encodings can typically not be auto-detected. You caniconvto do so. Example: if you have a PHP file in Latin-1 (ISO-8859-1) encoding, you can call:Note: you might have to use the
-l/--languageoption if the syntax can not be auto-detected bybat.Development
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 eitherregex-onigorregex-fancyas a feature when you depend onbatas a library.Contributing
Take a look at the
CONTRIBUTING.mdguide.Maintainers
Security vulnerabilities
See
SECURITY.md.Project goals and alternatives
battries to achieve the following goals:catThere 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.
batis 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.