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

Bat – A Cat Clone With Syntax Highlighting And Git Integration

By sk

In 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. In this brief guide, we will how to install and use Bat command in Linux.

Install Bat on 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

On Debian, Ubuntu, Linux Mint 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

On openSUSE:

$ sudo zypper install bat

Using Nix package manager:

$ nix-env -i bat

On Fedora, bat can be installed from the official Fedora Modular repository.

$ sudo dnf install bat

For other systems, you may need to compile and install from source. Make sure you have installed Rust 1.26 or higher.

Then, run the following command to install Bat:

$ cargo install bat

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? 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 3

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

bat command output 2

Git integration With Bat command

Customizing Bat

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 shells 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 project GitHub page given below.


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

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.


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. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More