Get to know bat in linux, the syntax highlighting cat

2 minutes reading
linux and devops

The other day I was looking for syntax highlighting tools and I came across a pretty interesting tool called bat (Yes, like bat in English), it's basically the linux cat command, but with colors and other pretty interesting functions. And, as icing on the cake, it's programmed in Rust. In this post I explain in a short way how it works and what it can do for you.

Install bat from repositories on linux

You can install it directly from the repositories in newer versions of Debian or derivative systems (Ubuntu).

sudo apt install bat

As my version of Debian is not the most recent one I had to install it from its version of software package with .deb ending. Remember that if you have problems with GNU/Linux commands go to read my entries where I make you a summary of the most basic GNU Linux commands

dpkg -i bat-musl_0.15.4_amd64.deb

What is bat for?

Bat will display the contents of a file, just like cat, but with the syntax highlighted for most programming languages:

Cat, the default tool shows us the file that we indicate but in a single color.

cat index.js
import React from "react"
import ReactDOM from "react-dom"
import App from "./App"

ReactDOM.render(<App/>, document.getElementById('root'))

If we now use bat we will see the language syntax highlighted and the line numbers in the standard output:

bat index.js
//File: index.js
import React from "react"
import ReactDOM from "react-dom"
import App from "./App"

ReactDOM.render(<App/>, document.getElementById('root'))

You can highlight tabs, spaces and line breaks using the -A option.

bat -A index.css
••margin: 0;␊
••background-color: whitesmoke;␊
••font-family: Lolita;␊
••font-color: #bbb;␊
••font-size: 20px;␊
••padding: 10px;␊

Bat has many topics available

Bat has themes for all tastes, hippies, sober, formal, psychedelic, etc. Use --list-themes to see them.

bat --list-themes
Theme: Monokai Extended

  // Output the square of a number.
  fn print_square(num: f64) {
      let result = f64::powf(num, 2.0);
      println!("The square of {:.2} is {:.2}.", num, result);

If you like a particular theme you can temporarily use it to display a file using the --theme option.

bat --theme='Monokai Extended' index.css

To load it permanently you must add it to your .bash_rc file.

# ~/.bashrc
export BAT_THEME="Monokai Extended"

Bat comes with an automatic pager.

This means that if the file is enormous, it will not show the whole file, but it will show a part of it and we will be able to move through the content with the arrows on our keyboard. The default pager is less.

# This file is autogenerated by pip-compile
# To update, run:
#    pip-compile --output-file requirements.txt

Bat has git integration

Bat integrates with git and shows you the horrible buggy changes you have implemented in your code. It adds a + symbol for added lines and a ~ for modified lines.

{% block content %}
  <header class="navbar" role="navigation">
    ~ <p>Parrafo modificado</p>
    + <p>Parrafo nuevo</p>
{% endblock %}

What do you think, would you use it? I find it a very useful tool, especially for environments where you don't have a graphical interface or if you are like me and don't like to wait for the IDE to load.

Related content