Home Command line utilities Bat – A Cat Clone With Syntax Highlighting And Git Integration

Bat – A Cat Clone With Syntax Highlighting And Git Integration

By sk
Published: Last Updated on 8.6K views

This guide explains what is Bat, how to install bat in Linux, and how to use Bat command in Linux and Unix operating systems.

What is Bat?

In Linux and Unix-like systems, the 'cat' command is used to print and concatenate files.

Using cat command, we can print the contents of a file to the standard output, concatenate several files into the target file, and append several files into the target file.

Today, I stumbled upon a similar utility named "Bat". It is just a clone to the cat command, with some additional cool features such as syntax highlighting, git integration and automatic paging etc

Install Bat on Linux

Bat is packaged for popular Linux operating systems.

Alpine Linux:

Bat is available in the official repositories of Alpine Linux. To install bat on Alpine Linux, run:

$ sudo apk add bat

Arch Linux:

Bat is available in the default repositories of Arch Linux. So, you can install it using pacman on any arch-based systems.

$ sudo pacman -S bat

Debian-based systems:

On Debian, Ubuntu, Linux Mint, Pop_OS! systems, download the .deb file from the Releases page and install it as shown below.

$ wget https://github.com/sharkdp/bat/releases/download/v0.11.0/bat_0.11.0_amd64.deb
$ sudo apt install gdebi
$ sudo gdebi bat_0.11.0_amd64.deb


You can instal bat with zypper like below:

$ sudo zypper install bat

Using Nix package manager:

In NixOS, you can install bat using nix package manager:

$ nix-env -i bat


Bat can be installed from the official Fedora Modular repository.

$ sudo dnf install bat


emerge sys-apps/bat

Void Linux:

You can install bat via xbps-install:

$ sudo xbps-install -S bat


You can install a precompiled bat package with pkg:

# pkg install bat

Or build it from the FreeBSD ports:

# cd /usr/ports/textproc/bat
# make install

Using Cargo package manager from source:

Make sure you have installed Rust 1.26 or higher.

Then, run the following command to install Bat:

$ cargo install bat

Using Linuxbrew:

Alternatively, you can install it using Linuxbrew package manager.

$ brew install bat

Bat command Usage

The Bat command's usage is very similar to cat command.

To create a new file using bat, do:

$ bat > file.txt

To view the contents of a file using bat, run:

$ bat file.txt

You can also view multiple files at once:

$ bat file1.txt file2.txt

To append the contents of the multiple files in a single file:

$ bat file1.txt file2.txt file3.txt > document.txt

Like I already mentioned, apart from viewing and editing files, the Bat utility has some additional cool features though.

Bat supports syntax highlighting for large number of programming and markup languages. For instance, look at the following example.

I am going to display the contents of the reverse.py file using both cat and bat commands.

cat and bat command output comparison
cat and bat command output comparison

Did you notice the difference? The cat command shows the contents of the file in plain text format, whereas Bat shows output with syntax highlighting, order number in a neat tabular column format. Much better, isn't it?

If you want to display only the line numbers (not the tabular column), use -n flag.

$ bat -n reverse.py

Sample output:

bat command output without tabular column
bat command output without tabular column

Another notable feature of Bat command is it supports automatic paging. That means if output of a file is too large for one screen, the bat command automatically pipes its own output to less command, so you can view the output page by page.

Let me show you an example.

When you view the contents of a file which spans multiple pages using cat command, the prompt quickly jumps to the last page of the file, and you do not see the content in the beginning or in the middle.

Have a look at the following output:

cat command output
cat command output

As you can see, the cat command displays last page of the file.

So, you may need to pipe the output of the cat command to less command to view it's contents page by page from the beginning.

$ cat reverse.py | less

Now, you can view output page by page by hitting the ENTER key. However, it is not necessary if you use bat command. The bat command will automatically pipe the output of a file which spans multiple pages.

$ bat reverse.py

Sample output:

Bat command supports automatic paging
Bat command supports automatic paging

Now hit the ENTER key to go to the next page.

The bat command also supports GIT integration, so you can view/edit the files in your Git repository without much hassle. It communicates with git to show modifications with respect to the index (see left side bar).

Git integration With Bat command
Git integration With Bat command

Customizing Bat command theme

If you don't like the default themes, you can change it too. Bat has option for that too.

To list the available themes, just run:

$ bat --list-themes
Monokai Extended
Monokai Extended Bright
Monokai Extended Light
Monokai Extended Origin

To use a different theme, for example TwoDark, run:

$ bat --theme=TwoDark file.txt

If you want to make the theme permanent, use export BAT_THEME="TwoDark" in your shell's startup file.

Bat also have the option to control the appearance of the output. To do so, use the --style option. To show only Git changes and line numbers but no grid and no file header, use --style=numbers,changes.

You can read the comparison of similar tools from this table. Please note that comparison is made from Bat's perspective.

For more details, refer the Bat command project GitHub page given below.


You May Also Like


Harish August 30, 2018 - 3:11 pm

Thank you! It is an awesome utility to view the source code. Sometimes using vim/gvim/sublime can be tiring. And cat doesn’t have syntax highlighting. I like how they have syntax highlighting and stuff. Line numbers are also great gifts.

Kamil March 16, 2022 - 2:19 pm

Thank you!


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More