= Drop the cat command for bat

The `cat` command on Linux concatenates files together.
Today, it's often used to concatenate one file to nothing, resulting in the single file's contents to be printed to the terminal.
It's a quick way to preview the contents of a text file without having to open the file in some large application.
There's nothing wrong with cat, but similar command have been developed over the years, and the one with the most features is https://github.com/sharkdp/bat[bat].

== Syntax highlighting

Bat, available under the terms of either the MIT License or the Apache
License 2.0 (your choice), is called by its developers a "cat clone with wings."
There's probably a healthy debate over what those wings are, exactly, but for me it's the syntax highlighting and line numbering.
It's a highly visual feature, but when you're scanning through files it can be a great help:

image:bat-syntax.jpg[The output of bat features line numbers and colorful syntax highlighting.]

Because you're likely to use the output of `bat` as the input for a second command, you can deactivate extra data like line numbers with the `--plain` option.
Syntax highlighting is retained on supported terminals.

The `--plain` option is actually an alias for `--style=plain`, which hints at just how many ways you can  customize the command's output.
Here are some of my favorite options for the `--style` option:

* plain: include no style that add extra characters to the output
* header: print the file name before the contents of the file
* header-filesize: print the file size before the contents of the file
* grid: add grid lines to separate output
* numbers: print line numbers

That's not all of the style options, just the ones I use frequently.

== Git integration

Did I say syntax highlighting was my favorite feature?
I meant that Git integration is my favorite feature.
The `bat` command is Git-aware by default.
When you're in a Git repository, you can use the `--diff` option to view just the changes to a file since it was last committed.

For instance, assume I'm in the Git repository of my zombie apocalypse game.
I've recently updated some code, but I can't quite recall the extent of it.

[source,bash]
----
$ bat --diff Zombie.java
1  public class Player {
2
3  private BufferedImage image;
4+ private Position pos;
5  private Health health;
6-
7  public Zombie() {
8    loadImage();
[...]
----

When a line has been added since the previous commit, a plus sign (`+`) appears in the margin, and when a line has been changed or removed, a minus sign (`-`) appears.

The `--diff` option only works with Git.
It's not a general-purpose https://opensource.com/article/21/11/linux-diff-patch[diff tool].

== Highlight arbitrary lines

You can highlight a range of lines in a file.
This isn't syntax highlighting, which `bat` does automatically.
Instead, this marks each line with a solid box, as if to mimic a highlighter that you use on paper.

[source,bash]
----
$ bat --highlight-line=20:26 ZombieCruncher.java
----

Your results may vary with this effect.
On some screens, I have a feeling that instead of highlighting this effect obfuscates due to a drop in contrast.
Still, it's a visual marker that might be useful to some:

image:bat-highlighter.jpg[The highlight effect changes the background of text.]

== Install bat

There are more features in bat than what I've covered here, but these are the ones I find myself using the most.
The `bat` command isn't strictly essential.
Commands like `cat` and `more`, and even `sed` or `awk` in a pinch, perform basically the same function, but `bat` consciously does it with a lot more style.
And that counts for something, if you like the visual aid of colorful output and a little extra context.

To install bat on Fedora, use the package manager:

[source,bash]
----
$ sudo dnf install bat
----

On other systems, you can download a binary release from the bat https://github.com/sharkdp/bat/releases[Git repository] or build it using the `crate` command.