📦 sharkdp / bat

A cat(1) clone with wings.

57.5k stars 1.5k forks 👁 57.5k watching ⚖️ Apache License 2.0
clicommand-linegithacktoberfestrustsyntax-highlightingterminaltool
📥 Clone https://github.com/sharkdp/bat.git
HTTPS git clone https://github.com/sharkdp/bat.git
SSH git clone git@github.com:sharkdp/bat.git
CLI gh repo clone sharkdp/bat
Loading files...
📄 README.md

bat - a cat clone with wings
Build Status license Version info
A cat(1) clone with syntax highlighting and Git integration.

Key FeaturesHow To UseInstallationCustomizationProject goals, alternatives
[English] [中文] [日本語] [한국어] [Русский]

Sponsors

A special thank you goes to our biggest sponsors:

Warp
Warp, the intelligent terminal
Available on MacOS, Linux, Windows

Syntax highlighting

bat supports syntax highlighting for a large number of programming and markup languages:

Syntax highlighting example

Git integration

bat communicates with git to show modifications with respect to the index (see left side bar):

Git integration example

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.md

Display multiple files at once

bat src/*.rs

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)

curl -s https://sh.rustup.rs | bat

Read from stdin, specify the language explicitly

yaml2json .travis.yml | json_pp | bat -l json

Show and highlight non-printable characters:

bat -A /etc/hosts

Use it as a cat replacement:

bat > note.md  # quickly create a new file

bat 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 log

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.

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 | xclip
bat 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 the MANPAGER environment variable:

export MANPAGER="bat -plman"
man 2 select
(replace bat with batcat if 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 / rustfmt

The prettybat script is a wrapper that will format code and print it with bat.

Warp

Warp

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.

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

Packaging status

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 bat

Important: If you install bat this way, please note that the executable may be installed as batcat instead of bat (due to a name clash with another package). 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/bat %%CODEBLOCK19%%bash alias bat="batcat" %%CODEBLOCK20%%bash sudo dpkg -i bat_0.18.3_amd64.deb # adapt version number and architecture %%CODEBLOCK21%%bash apk add bat %%CODEBLOCK22%%bash pacman -S bat %%CODEBLOCK23%%bash dnf install bat %%CODEBLOCK24%%bash emerge sys-apps/bat %%CODEBLOCK25%%bash pkg install bat %%CODEBLOCK26%%bash cd /usr/ports/textproc/bat make install %%CODEBLOCK27%%bash pkg_add bat %%CODEBLOCK28%%bash nix-env -i bat %%CODEBLOCK29%%bash zypper install bat %%CODEBLOCK30%%bash brew install bat %%CODEBLOCK31%%bash port install bat %%CODEBLOCK32%%bash winget install sharkdp.bat %%CODEBLOCK33%%bash choco install bat %%CODEBLOCK34%%bash scoop install bat %%CODEBLOCK35%%bash cargo install --path . --locked %%CODEBLOCK36%%bash cargo install --locked bat %%CODEBLOCK37%%bash bat --completion <shell> # see --help for supported shells %%CODEBLOCK38%%bash bat --list-themes | fzf --preview="bat --theme={} --color=always /path/to/file" %%CODEBLOCK39%%bash 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 %%CODEBLOCK40%%bash bat cache --build %%CODEBLOCK41%%bash bat cache --clear %%CODEBLOCK42%%bash 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 %%CODEBLOCK43%%bash --map-syntax='*.conf:INI' %%CODEBLOCK44%%bash --map-syntax='.ignore:Git Ignore' %%CODEBLOCK45%%bash --map-syntax='/etc/apache2/**/*.conf:Apache Conf' %%CODEBLOCK46%%bash export BAT_PAGER="less -RFK" %%CODEBLOCK47%%bash alias cat="bat --theme auto:system --theme-dark default --theme-light GitHub" %%CODEBLOCK48%%bash # .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' %%CODEBLOCK49%%bash bat --config-file %%CODEBLOCK50%%bash export BAT_CONFIG_PATH="/path/to/bat/bat.conf" export BAT_CONFIG_DIR="/path/to/bat" %%CODEBLOCK51%%bash bat --generate-config-file %%CODEBLOCK52%%bash # 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++" %%CODEBLOCK53%%bash 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[@]}" } %%CODEBLOCK54%% bash iconv -f ISO-8859-1 -t UTF-8 my-file.php | bat %%CODEBLOCK55%%bash # 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](https://docs.rs/bat/). Note that you have to use either regex-onig or regex-fancy as a feature when you depend on bat as a library. ## Contributing Take a look at the [CONTRIBUTING.md](CONTRIBUTING.md) guide. ## Maintainers - [sharkdp](https://github.com/sharkdp) - [eth-p](https://github.com/eth-p) - [keith-hall](https://github.com/keith-hall) - [Enselic](https://github.com/Enselic) ## Security vulnerabilities See [SECURITY.md](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 interface There are a lot of alternatives, if you are looking for similar programs. See [this document](doc/alternatives.md) for a comparison. ## License Copyright (c) 2018-2025 [bat-developers](https://github.com/sharkdp/bat). 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.