Despite the title of this article, we’ll not talk about cats and bats here but about the cat
and bat
commands in Linux.
As you know, the cat
(short for concatenate) command is a utility in Linux. One of its most commonly known usages is to print the content of a file onto the standard output stream. But given more time spent in the command line, features like syntax highlighting are convenient.
Related: 20 Basic Linux Commands for Beginners Explained with Examples
What’s bat Command
Bat is a drop-in replacement for the cat
command, with additional cool features such as syntax highlighting, git integration, and automatic paging.
By default, bat
pipes its output to a pager (e.g., less
) if the output is too large for one screen.
However, if you would instead bat
work like the cat
command all the time (never page output), you can set --paging=never
as an option, either on the command line or in your configuration file.
The bat
command also allows you to search during output (if the output is longer than the screen height) using the /
key binding, similarly to less
searching.
Bat uses the syntect library for syntax highlighting. Syntect is a popular syntax highlighting library for Rust that uses Sublime Text syntax definitions.
It is possible to alias cat
directly to bat
in your shell configuration. To do this, add the following line to your ~/.bashrc
file:
alias cat='bat --paging=never'
Code language: JavaScript (javascript)
Then source the file:
source ~/.bashrc
Like cat
command, bat
works out-of-the-box on Linux. No extra configuration is needed. So, to use it, all you have to do is type:
bat filename
The bat
command receives our strongest recommendation. It is a helpful utility that you’ll wonder how you managed without it.
Bat Highlights
- Syntax highlighting: Bat supports syntax highlighting for many programming and markup languages.
- Git integration: Bat communicates with git to show modifications with respect to the index.
- Automatic paging: The command can pipe its own output to
less
if it is too large for one screen. - Display and highlight non-printable characters: You can use the
-A
option to show and highlight non-printable characters. - Including all of the
cat
command functionality.
You see the difference. The cat
command shows the file’s contents in plain text format, whereas the bat
command shows the output with syntax highlighting and ordering row numbers in a neat tabular column format.
Those interested in learning more about the bat
command in Linux can visit the project’s GitHub page.
Installing the bat Command in Linux
We should utilize the system package manager. Let’s look at several well-known Linux distribution examples for installing the bat command.
If your Ubuntu / Debian installation is new enough, you can run:
sudo apt install bat
Arch Linux users can install the bat
package from Arch’s official repo:
sudo pacman -S bat
To install the bat
command on Fedora, type:
sudo dnf install bat
You can install bat
with zypper
on openSUSE:
sudo zypper install bat
Conclusion
This short article covered how to install and use the bat
command on Linux. In my opinion, the bat
command is an excellent modern tool for Linux users. So, give it a shot and let us know what you think.