Please visit the original link: sysin.org/blog/bat/ for the latest version. Original works, reproduced please retain the source.

Author: GC (at)sysin.org, homepage: www.sysin.org

Introduction to the

Bat – A cat clone with wings 😄

Someone translated it as “a flying cat.”

The cat (concatenate) command is used to connect files and print them to the standard output device. This is used to create, view and connect files.

Bat is a cat clone that supports syntax highlighting and Git integration. Is used to replace cat and enhanced cat commands.

Project homepage: github.com/sharkdp/bat

Syntax highlighting

Bat supports syntax highlighting for a wide range of programming and markup languages (see Changelog for details, as well as customizing syntax support or submitting requests to authors) :

Git integration

Bat can communicate with Git to show changes to the index (see left column) :

Displays non-printable characters

You can use the -a /–show-all option to display and highlight non-printable characters:

Automatic paging

By default, if the output is too large for one screen, BAT controls its own output pipeline (for example, less). If you want bat to always work like CAT (output from never pages), you can set — Paging =never as an option on the command line or in the configuration file. If you intend to alias cat to bat in your shell configuration, you can use alias cat=’bat –paging=never’ to retain the default behavior.

The file

Even with the paging option set, you can still use bat to connect to the file 😉. Whenever BAT detects a non-interactive terminal (that is, when you pipe into another process or file), BAT will act as a substitute for CAT and fall back to printing pure file content, regardless of the value of the –pager option.

The installation

macOS

brew install bat
Copy the code

FreeBSD

pkg install bat
Copy the code

As in most cases, packages shipped on Unix tend to be current versions, while Linux is often old or not shipped.

Ubuntu

apt install bat
#Note: After installation, the command is batcat, and the version is older
Copy the code

You can visit the Release page to get the latest version:

Wget https://github.com/sharkdp/bat/releases/download/v0.18.0/bat_0.18.0_amd64.deb DPKG -i bat_0. 18.0 _amd64. DebCopy the code

CentOS

Wget tar ZXVF - https://github.com/sharkdp/bat/releases/download/v0.18.0/bat-v0.18.0-x86_64-unknown-linux-musl.tar.gz Bat -v0.18.0-x86_64-unknown-linux-musl.tar.gz CD bat-v0.18.0-x86_64-unknown-linux-musl cp./bat /usr/local/bin cp./bat  /usr/local/share/man/man1/ mandbCopy the code

See the project home page for more binary format and source code installation.

Directions for use

You can configure the alias to replace cat directly with bat.

  • bash
echo 'alias cat="bat"' >> ~/.bashrc
Copy the code
  • zsh
echo 'alias cat="bat"' >> ~/.zshrc
Copy the code

Here are some examples:

  • Displays a single file on a terminal
bat README.md
Copy the code
  • Display multiple files at once
bat src/*.rs
Copy the code
  • Read from STdin, automatically detect syntax
curl -s https://sh.rustup.rs | bat
Copy the code
  • Read from stdin, specifying the language explicitly
yaml2json .travis.yml | json_pp | bat -l json
Copy the code
  • Displays and highlights non-printable characters:
bat -A /etc/hosts
Copy the code
  • As acatAlternatives to:
Md > footer. Md > document.md # Merge file bat -n main.rs # display line count (only) bat f-g # First output 'f', then stdin, then 'g'.Copy the code