DEV Community

Ben Santora
Ben Santora

Posted on

Linux OS - The 'bat' utility

If you use the Linux operating system (or Unix), you've probably used 'cat' - a part of the GNU core utilities (coreutils) package. The cat command concatenates and displays file content, perfect for quickly viewing or merging files.

In 2016, David Peter (Sharkdp) created 'bat' - a clone of cat, but with enhancements that include syntax highlighting, Git integration, and line numbers, creating a more advanced and user-friendly alternative. Let's get it set up in WSL.

Download the latest release of 'bat'

curl LO https://github.com/sharkdp/bat/releases/download/v0.19.0/bat-v0.19.0-x86_64-unknown-linux-musl.tar.gz
Enter fullscreen mode Exit fullscreen mode

Extract the tar

tar -xzf bat-v0.19.0-x86_64-unknown-linux-musl.tar.gz
Enter fullscreen mode Exit fullscreen mode

Move the binary to /usr/bin

sudo mv bat-v0.19.0-x86_64-unknown-linux-musl/bat /usr/bin/
Enter fullscreen mode Exit fullscreen mode

Make the binary executable

sudo chmod +x /usr/bin/bat
Enter fullscreen mode Exit fullscreen mode

Verify installation

bat --version
Enter fullscreen mode Exit fullscreen mode

Test bat with file

bat ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

For bat to show syntax highlighting, the file needs to contain recognized patterns for a specific language or format - ie - Keywords and Commands / Variables / File Extensions / Shebang Lines / Etc

Highlighting works best with structured code or configuration files. General text might not show much highlighting. So, bat is perfect for code and scripts but might be overkill for plain text.

Line numbers in bat are enabled by default. Toggle them with the --number flag if they’re not showing:

bat --number <filename>
Enter fullscreen mode Exit fullscreen mode

Ben Santora - October 2024

Top comments (0)