Skip to content

[Linux] Use the batcat Command as a Replacement for cat, Highlighting Code or Configurations

Last Updated on 2024-10-02 by Clay

batcat or simply bat, is a replacement tool for the cat command. It retains the functionality of cat for displaying files, while also highlighting keywords in code or configuration files, making it more convenient for developers to browse daily tasks or code files (thus, it's definitely a productivity tool!).

bat is a command-line tool developed in Rust, featuring the following:

  1. Syntax Highlighting: bat has built-in syntax highlighting support for many programming languages, similar to editors like less or nano, making code viewing more intuitive.
  2. Built-in Pagination: Similar to less, bat will automatically enable pagination when the content is too long, making it more readable; this is very useful for viewing large files.
  3. Line Numbers: It can display line numbers, making it easier to locate when reading or editing code.
  4. File Differences Integration: When bat displays multiple files simultaneously, it highlights the differences, helping users understand changes more clearly.
  5. Git Integration: bat marks changes in the file that have not been committed to Git, allowing developers to see modifications at a glance.

Installation

bat supports multiple Linux distributions and can also be used on Windows and macOS.
Here are some installation methods:

Ubuntu/Debian

sudo apt install bat



Arch Linux

sudo pacman -S bat



macOS (Homebrew)

brew install bat



Windows (Scoop)

scoop install bat

Usage

The native command might be bat or batcat, as some Linux distributions may already have a tool named bat. In such cases, you should use the batcat command, so it's good to verify before use.

Here, I will use bat for all examples.

bat works much like cat, automatically detecting the language and format of the file:

bat test.py


Output:

This makes it much easier to read the code content.

Moreover, if the file content is quite long, you can read it in paginated mode:

bat --paging=always filename.txt


In this mode, you can even use search commands like in less or vim to navigate to the desired section.

Additionally, if you want to specify the syntax highlighting format, you can use the --language parameter.

bat --language python script.txt

References


Read More

Tags:

Leave a Reply