bat README

The project's README from github.com/sharkdp/bat. As one markdown file: README.md.

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

Key Features • How To Use • Installation • Customization • Project goals, alternatives
[English] [中文] [日本語] [한국어] [Русский]

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 sidebar):

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

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

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.

[!TIP] To remove these abbreviations later, run:

abbr -e -- --help
abbr -e -- -h

The -- before the abbreviation name is required because --help and -h start with dashes, which would otherwise be interpreted as flags to abbr itself.

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: On some older Ubuntu/Debian releases, the executable is installed as batcat instead of bat (due to a name clash with another package). On newer releases, the executable is available as bat. 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:

mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat

an example alias for batcat as bat:

alias bat="batcat"

On Ubuntu (using most recent .deb packages)

... 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 .deb package from the release page and install it via:

sudo dpkg -i bat_0.18.3_amd64.deb  # adapt version number and architecture

On Alpine Linux

You can install the bat package from the official sources, provided you have the appropriate repository enabled:

apk add bat

On Arch Linux

You can install the bat package from the official sources:

pacman -S bat

On Fedora

You can install the bat package from the official sources:

dnf install bat

On Gentoo Linux

You can install the bat package from the official sources:

emerge sys-apps/bat

On FreeBSD

You can install a precompiled bat package with pkg:

pkg install bat

or build it on your own from the FreeBSD ports:

cd /usr/ports/textproc/bat
make install

On OpenBSD

You can install bat package using pkg_add(1):

pkg_add bat

Via nix

You can install bat using the nix package manager:

nix-env -i bat

On openSUSE

You can install bat with zypper:

zypper install bat

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 bat with Homebrew:

brew install bat

On macOS via MacPorts

Or install bat with MacPorts:

port install bat

On Windows

There are a few options to install bat on Windows. Once you have installed bat, take a look at the "Using bat on Windows" section.

Prerequisites

You will need to install the Visual C++ Redistributable

With WinGet

You can install bat via WinGet:

winget install sharkdp.bat

With Chocolatey

You can install bat via Chocolatey:

choco install bat

With Scoop

You can install bat via scoop:

scoop install bat

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 bat for many different architectures. Statically-linked binaries are also available: look for archives with musl in the file name.

From source

If you want to build bat from source, you need Rust 1.79.0 or higher. You can then use cargo to build everything:

From local source

cargo install --path . --locked

[!NOTE] The --path . above specifies the directory of the source code and NOT where bat will be installed. For more information see the docs for cargo install.

From crates.io

cargo install --locked bat

Note 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 cargo and should be available in the cargo target folder under build.

Furthermore, shell completions are also available by running:

bat --completion <shell>
# see --help for supported shells

Customization

Highlighting theme

Use bat --list-themes to get a list of all available themes for syntax highlighting. By default, bat uses Monokai Extended or Monokai Extended Light for dark and light themes respectively. To select the TwoDark theme, call bat with the --theme=TwoDark option or set the BAT_THEME environment variable to TwoDark. Use export BAT_THEME="TwoDark" in your shell's startup file to make the change permanent. Alternatively, use bat's configuration file.

If you want to preview the different themes on a custom file, you can use the following command (you need fzf 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. You can use the --theme-dark / --theme-light options or the BAT_THEME_DARK / BAT_THEME_LIGHT environment 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

bat has three themes that always use 8-bit colors, even when truecolor support is available:

Although these themes are more restricted, they have three advantages over truecolor themes. They:

Output style

You can use the --style option to control the appearance of bat'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 the BAT_STYLE environment variable to make these changes permanent or use bat's configuration file.

By default, bat enables changes, grid, header-filename, numbers, and snip.

The available pre-defined styles are:

Style Description
default Enables the recommended style components listed above.
full Enables all available components.
auto Same as default, unless the output is piped.
plain Disables all available components.

The available individual components are:

Component Description
changes Show Git modification markers.
header Alias for header-filename.
header-filename Show filenames before the content.
header-filesize Show file sizes before the content.
grid Vertical/horizontal lines to separate the side bar and header from the content.
rule Horizontal lines to delimit files.
numbers Show line numbers in the side bar.
snip Draw separation lines between distinct line ranges.

[!tip] If you specify a default style in bat's config file, you can change which components are displayed during a single run of bat using the --style command-line argument. By prefixing a component with + or -, it can be added or removed from the current style.

For example, if your config contains --style=full,-snip, you can run bat with --style=-grid,+snip to remove the grid and add back the snip component. Or, if you want to override the styles completely, you use --style=numbers to only show the line numbers.

Decorations

By default, bat 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.

There is also the --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.

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 current bat installation.

bat uses the excellent syntect library for syntax highlighting. syntect can read any Sublime Text .sublime-syntax file and theme.

A good resource for finding Sublime Syntax packages is Package Control. Once you found a syntax:

  1. Create a folder with syntax definition files:

    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, use bat --list-languages to 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 in bat by 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 de

bat (sharkdp/bat)

Searching Database…

60.8K ·GitHub
这是什么?bat 是一个 cat(1) 的克隆,可以在终端里带语法高亮地显示文件,支持大量编程和标记语言,还能在侧边显示 Git 修改标记。
它是图形界面程序,还是纯终端命令?它是一个命令行工具。当检测到非交互式终端时(比如把输出通过管道传给另一个进程或写入文件),它就充当 cat 的直接替代品,输出纯文本内容。
最快把 bat 装到我电脑上的方法是什么?用你系统的包管理器:Ubuntu 20.04+/Debian 11+ 上用 apt,macOS 和 Linux 上用 brew,Windows 上用 winget/Chocolatey/Scoop,或者直接从发布页下载预编译的二进制文件。
还需要一起安装别的东西吗?它是一个单独的静态二进制文件,不过在 Windows 上需要 Visual C++ Redistributable;如果要自己从源码编译,需要 Rust 1.79.0 或更高版本。
刚装完有什么要注意的坑吗?在一些较老的 Ubuntu/Debian 版本上,由于名称冲突,可执行文件会安装成 batcat 而不是 bat;可以先试试 batcat --version,然后建一个 bat -> batcat 的符号链接或设置别名。
它能在 macOS、Windows 和各种 Linux 发行版上运行吗?可以:支持 Ubuntu/Debian、Alpine、Arch、Fedora、Gentoo、openSUSE 和 Nix 的软件包,macOS 可通过 Homebrew 或 MacPorts 安装,Windows 可通过 WinGet、Chocolatey 或 Scoop 安装,此外还有 FreeBSD 和 OpenBSD 版本。
为什么输出很长时有时会打开分页器?默认情况下,当文件超过一屏时,bat 会把输出交给 less 这类分页器;想让内容始终直接打印,可以用 --paging=never,或者设置别名 cat='bat --paging=never'。
从标准输入读取内容时语法高亮还有效吗?只有当语法能从第一行判断出来时才有效,通常靠 #!/bin/sh 这样的 shebang;否则需要显式指定语法,例如 bat -l log。
可以换一个配色主题吗?可以:先运行 bat --list-themes 预览各个主题,然后用 --theme 或 BAT_THEME 环境变量设置;bat 还会根据你终端的背景色自动挑选一个合适的主题。
它和其他终端工具配合得好吗?README 里介绍了 bat 的多种用法:作为 fzf 的预览器、通过 batgrep 给 ripgrep 的搜索结果上色、作为 man 手册页带颜色的 MANPAGER、配合 find/fd 做预览,以及配合 git show 和 git diff 使用。
What are similar projects?vis, delta, diff-so-fancy, fish-shell, linguist, gitlogue, git, redox
Creator
sharkdp
Size
34.4 MB
Stars
60.8K
Age
8y
Commits · 90 Days