Bat is Like the cat Command in Linux, But Super-Charged and Written in Rust

Bat is a cat command clone with advance syntax highlighting for a large number of programming and markup languages.

3
bat Command in Linux

Bat is a cat command clone with advance syntax highlighting for a large number of programming and markup languages.

Despite the title of this article, we’ll not talk about cats and bats here, but about the cat and bat commands in Linux.

As you know, the cat (short for concatenate) command is a utility in Linux. One of its most commonly known usages is to print the content of a file onto the standard output stream. But given more time spent in the command line, features like syntax highlighting come in very handy.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

What’s bat Command

Bat is a drop-in replacement for the cat command, with some additional cool features such as syntax highlighting, git integration and 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.

The bat command also allows you to search during output (if the output is longer than the screen height) using the / key binding, similarly to less searching.

Bat uses the syntect library for syntax highlighting. Syntect is a popular syntax highlighting library for Rust that uses Sublime Text syntax definitions.

It is possible to alias cat directly to bat in your shell configuration. To do this, add the following line to your ~/.bashrc file:

alias cat='bat --paging=never'

Then source the file:

source ~/.bashrc

Like cat command, bat just works out-of-the-box on Linux. No extra configuration needed. So, in order to use it, all you have to do is type:

bat filename

The bat command receives our strongest recommendation. It is such a useful utility that you’ll wonder how you managed without it.

Bat Highlights

  • 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.
  • Automatic paging: The command can pipe its own output to less if the output is too large for one screen.
  • Display and highlight non-printable characters: You can use the -A option to show and highlight non-printable characters.
  • Including all of the cat command functionality.
The bat Linux Command Compared to cat

You see the difference. The cat command shows the contents of the file in plain text format, whereas bat shows output with syntax highlighting and order row number in a neat tabular column format. 

Those interested in learning more about bat can visit the project’s GitHub page.

Install bat in Linux

If your Ubuntu / Debian installation is new enough you can simply run:

sudo apt install bat

Arch Linux users can install the bat package from the Arch’s official repo:

sudo pacman -S bat

In order to install the bat command on Fedora type:

sudo dnf install bat

You can install bat with zypper on openSUSE:

sudo zypper install bat

3 Comments

Leave a Reply

Your email address will not be published.

Latest from News