(Last Updated On: September 10, 2018)

Bat is a cat(1) clone with syntax highlighting and Git integration. If you need ana 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 clone 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.

How to 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,

sudo apt-get install wget
export VER="0.6.1"
wget https://github.com/sharkdp/bat/releases/download/v${VER}/bat_${VER}_amd64.deb
sudo dpkg -i bat_${VER}_amd64.deb

Sample installation output:

Selecting previously unselected package bat.
(Reading database ... 114128 files and directories currently installed.)
Preparing to unpack bat_0.6.1_amd64.deb ...
Unpacking bat (0.6.1) ...
Setting up bat (0.6.1) ...
Processing triggers for man-db (2.8.3-2) ...

How to install bat 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

How to install bat on Void Linux

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

xbps-install -S bat

Install bat On macOS

You can install bat with Homebrew:

brew install bat

How to use bat

Here are few examples on how you can use bat

To view contents of a file, use:

$ bat <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.6.1
A cat(1) clone with syntax highlighting and Git integration.

USAGE:
    bat [OPTIONS] [FILE]...
    bat <SUBCOMMAND>

OPTIONS:
    -l, --language <language>
            Explicitly set the language for syntax highlighting. The language can be
            specified as a name (like 'C++' or 'LaTeX') or possible file extension (like
            'cpp', 'hpp' or 'md'). Use '--list-languages' to show all supported language
            names and file extensions.
        --list-languages
            Display a list of supported languages for syntax highlighting.

        --theme <theme>
            Set the theme for syntax highlighting. Use '--list-themes' to see all
            available themes. To set a default theme, export the BAT_THEME environment
            variable (e.g.: export BAT_THEME="TwoDark").
        --list-themes
            Display a list of supported themes for syntax highlighting.

        --style <style-components>
            Configure which elements (line numbers, file headers, grid borders, Git
            modifications, ..) to display in addition to the file contents. The argument
            is a comma-separated list of components to display (e.g.
            'numbers,changes,grid') or a pre-defined style ('full') [default: auto]
            [possible values: auto, full, plain, changes,
            header, grid, numbers]
    -p, --plain
            Only show plain style, no decorations. This is an alias for '--style=plain'

    -n, --number
            Only show line numbers, no other decorations. This is an alias for
            '--style=numbers'
        --line-range <N:M>
            Only print the specified range of lines for each file. For example:
              '--line-range 30:40' prints lines 30 to 40
              '--line-range :40' prints lines 1 to 40
              '--line-range 40:' prints lines 40 to the end of the file
        --color <when>
            Specify when to use colored output. The automatic mode only enables colors if
            an interactive terminal is detected. [default: auto]  [possible values:
            auto, never, always]
        --decorations <when>
            Specify when to use the decorations that have been specified via '--style'.
            The automatic mode only enables decorations if an interactive terminal is
            detected. [default: auto]  [possible values: auto, never,
            always]
        --paging <when>
            Specify when to use the pager. To control which pager is used, set the PAGER
            or BAT_PAGER environment variables (the latter takes precedence). The default
            pager is 'less'. To disable the pager permanently, set BAT_PAGER to an empty
            string. [default: auto]  [possible values: auto, never,
            always]
        --wrap <mode>
            Specify the text-wrapping mode. [default: character]  [possible values:
            character, never]
    -u
            This option exists for POSIX-compliance reasons ('u' is for 'unbuffered'). The
            output is always unbuffered - this option is simply ignored.
    -h, --help
            Print this help message.

    -V, --version
            Show version information.


ARGS:
    <FILE>...
            File(s) to print / concatenate. Use a dash ('-') or no argument at all to read
            from standard input.

SUBCOMMANDS:
    cache
            Modify the syntax-definition and theme cache

For further customizations, check the official Bat Github page.