Bat – Linux cat command with syntax highlighting and Git integration

Posted on 11 views

Bat is a cat(1) clone with syntax highlighting and Git integration. If you need an amazing experience while working with the cat command, then this tool is definitely for you. Bat supports syntax highlighting for a large number of programming and markup languages.

Features of bat – cat alternative tool

  • Automatic paging:  bat can pipe its own output to less if the output is too large for one screen.
  • Git integration:  bat communicates with git to show modifications with respect to the index
  • Syntax highlighting: Native support for syntax highlighting for a large number of programming and markup languages:
  • File concatenation: Whenever bat detects a non-interactive terminal, it will fall back to printing the plain file contents.

Install bat on Ubuntu / Debian

There is a bat Debian package available on Github releases page. Check the latest release version before downloading with this method.

rm -f bat*
curl -s  https://api.github.com/repos/sharkdp/bat/releases/latest |grep browser_download_url |  cut -d '"' -f 4 | grep 'amd64.deb' | grep -v musl | wget -i -
sudo dpkg -i bat_*_amd64.deb

Sample installation output:

Selecting previously unselected package bat.
(Reading database ... 210772 files and directories currently installed.)
Preparing to unpack bat_0.21.0_amd64.deb ...
Unpacking bat (0.21.0) ...
Setting up bat (0.21.0) ...
Processing triggers for man-db (2.10.2-1) ...

You can also install directly using package manager. This won’t guarantee installation of the latest release:

sudo apt install bat

Install bat command on Arch Linux

For Arch Linux users, bat package is available on upstream repositories and you can install it using pacman package manager.

sudo pacman -S bat

Install bat command on Void Linux

On a Void Linux system, install bat using the following commands:

xbps-install -S bat

Install bat on Alpine Linux

sudo apk add bat

Install bat command on macOS

You can install bat with Homebrew:

brew install bat

Install bat command on Fedora

For Fedora perform the installation using dnf package manager:

sudo dnf install bat

Install bat command on Gentoo

Use the command:

sudo emerge sys-apps/bat

Install bat command on FreeBSD:

sudo pkg install bat

How to use bat command on Linux

Here are few examples on how you can use bat

To view contents of a file, use:

$ bat 

bat-cat-file

Display multiple files at once

$ bat code/*.yml

You can also read  input from stdin, explicitly specify the language

$ yaml2json playbook.yml | json_pp | bat -l json
curl -s https://site.com/main.rs | bat -l rs

To see all supported languages, use:

bat --list-languages

All other standard cat commands should work, e.g:

Creating a new file

bat > README.md

Combine three files output to one

$ bat file1.md file1.md file1.md > mydocument.md

Show file

$ bat -n ping_servers.sh
$ bat f - g # output 'f', then stdin, then 'g'.

Change Highlighting theme

To get a list of all available themes for syntax highlighting, run:

$ bat --list-themes

Set a theme by calling bat command with the --theme option.

$ bat --theme=DarkNeon

The same can be achieved by using BAT_THEME environment variable.

export BAT_THEME="DarkNeon"

This can be added to your ~/.bashrc | ~/.zshrc file

Check complete help page using

$ bat --help
bat 0.21.0 (405e5f7)
A cat(1) clone with syntax highlighting and Git integration.

USAGE:
    bat [OPTIONS] [FILE]...
    bat 
....

For further customizations, check the official Bat Github page.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.