Open In App

Bat – A Cat Clone with Syntax Highlighting and Git Integration

Last Updated : 10 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The ‘bat’ command is essentially a counterpart to the standard Linux command, ‘cat‘. ‘Cat is a default command found in most Linux distributions, primarily used for tasks such as text viewing and file concatenation. However, ‘bat‘ surpasses the ‘cat‘ command in several key aspects. It encompasses all the fundamental functionalities of ‘cat‘ and incorporates modern features like syntax highlighting, Git integration, and various other command-line tools.

Features:

  1. Syntax Highlighting: Bat provides syntax highlighting for a wide range of programming and markup languages. This makes it easier to read and understand the content of source code files or any other text files with structured content.
  2. Line Numbers: It displays line numbers by default, helping users quickly locate and reference specific lines within a file.
  3. Git Integration: Bat can display Git modifications and file metadata directly in the output. This can be especially helpful when reviewing changes in Git repositories.
  4. Customizable Themes: Users can customize the color themes used for syntax highlighting to suit their preferences.
  5. Automatic Paging: Like the “cat” command, Bat can display the content of files, but it also includes automatic paging, allowing users to scroll through the content one screen at a time. You can navigate through the content using arrow keys, making it easier to read long files.

How to Install Bat (A Cat Clone) in Linux?

If you are using Linux, most likely your distro repository will contain the bat package, you can simply install it from there. You can also install via .deb packages hosted on the release page of the Bat GitHub repository.

Step 1: For Debian and Ubuntu-based distros, use the following command.

sudo apt install bat
Installing bat command in Ubuntu

Installing the bat command in Ubuntu

Step 2: On Debian and Ubuntu, the bat uses the batcat command by default because of a conflict with an existing package, bacula-console-qt. You can, however, use the following commands to link the bat command:

  mkdir -p ~/.local/bin
  ln -s /usr/bin/batcat ~/.local/bin/bat

Note: In case you want to use the bacula-console-qt package, you should not use the above commands, stick to batcat command, whereever We have used bat, use batcat instead.

Linking batcat with bat

Linking batcat with bat

Step 3: For Fedora-based distros, use the following command.

sudo dnf install bat

How to Use Bat (A Cat Clone) in Linux?

Below are the practical examples and usage of the Bat tool in Linux.

1. To Display a single file

Create a file with your preferred text editor and add some content to it. We have added a file with Hello World in Java. To display your file, use the following command.

bat filename
Bat with single file

Bat with a single file

2. Displaying multiple files with bat

To display multiple files at once, we can make use of regex in the command.

bat ./*.java
Multiple files with bat

Multiple files with bat

3. Syntax Highlighting with Bat

Step 1: Bat offers a range of syntax highlighting themes and the flexibility to specify distinct themes for syntax highlighting. Additionally, it enables you to incorporate syntax highlighting for a language that is not inherently supported by the bat command. To get the list of themes supported by the bat command, use the following command.

bat --list-themes
Inbuilt Themes

Inbuilt Themes

Step 2: To get the output of the bat with the preferred theme, you have to pass the theme name to the bat command as a flag.

bat --theme=theme_name filename
Syntax highlighting with dracula theme

Syntax highlighting with dracula theme

Step 3: With the above method, you have to pass the theme flag whenever you run the bat command. If you think that is a hassle, you can set an environment variable called BAT_THEME, so that the theme you have specified will be used whenever you run the bat command. To set the theme for the specific shell session, you can just export the environment variable using the below command.

export BAT_THEME=Dracula

Step 4: To set the theme as a permanent one, append the below line in your .bashrc file located in your home directory.

export BAT_THEME=Dracula
Bat theme without passing as a flag

Bat theme without passing as a flag

3. Adding Languages for Syntax Highlighting

To view the list of languages that are supported by Bat for syntax highlighting, use the command given below.

bat --list-languages
Languages supported by bat

Languages supported by bat

Bat supports Sublime Text syntax files .sublime-syntax, to extend the languages support. The below steps you to add extra language support for bat.

Step 1: Create the syntax folder for bat config in your home directory.

mkdir -p "$(bat --config-dir)/syntaxes"
Creating syntax folder for bat

Creating a syntax folder for bat

Step 2: Enter into the bat config syntaxes directory.

cd "$(bat --config-dir)/syntaxes"
Moving into the syntax folder

Moving into the syntax folder

Step 3: Clone the git repo that hosts the syntax files or copy the syntax files here.

git clone "repo_link where the syntax file hosted or copy here"
cConing the repo containing the sublime syntax files

cloning the repo containing the sublime syntax files

Step 4: Let the bat command parse the syntax files

bat cache --build
Building the bat cache with the sublime-syntax

Building the bat cache with the sublime-syntax

Step 5: Check whether everything works fine, by cross-checking the list of languages supported by bat

bat --list-langauges | grep "language name you are added"
Checking the list of languages supported by bat

Checking the list of languages supported by bat

If you can find some output from grep, then you have successfully added support for the specific language that is not natively supported by the bat command.

4. Git Integration

Step 1: Bat command can integrate with git very well. To use it with git, you should have already installed git and should have a basic understanding of git, like how to commit works and logs. To see it working, create a git repository add a file to it, and commit the changes, like the below one.

git status
git add *
git commit -m "initial commit"
Sample git repo

Sample git repo

Step 2: Now we have committed our changes with the addition of two files. Let’s edit one of the files and see the changes with the bat command.

vim Hello.java
bat Hello.java
Bat integration with git

Bat integration with git

With the above image, we can see that the bat command highlights the changes that we have made to the file in the side margin. As line number 2,3,4,5 is added indicated with a “+” in the side margin and as line number 7 is modified it has a “~” in the side margin, it is indicated with the bat output with the help of git integration.

Step 3: To view the specific git commit message and the message from git show with bat, use the following command.

git show commit_id | bat -l rs
Bat with git show

Bat with git show

Step 4: To view only the file content in a specific version, pass the file path after the commit_id to view only the file content. Like below.

git show commit_id:file_path | bat -l rs
Viewing a specific file from git show

Viewing a specific file from git show

6. No paging

By default, bat pipes its output to a pager-like “less“, if the output is too large for a screen. But the cat won’t be doing that, it will just print it without paging. In case you intend to use “bat” with the behavior of the “cat” command. Use the following command.

bat --paging=never filename
Bat paging

Bat paging

7. Show non-printable characters.

Use the -A or –show-all flag for the non-printable characters.

bat --show-all filename 
bat -A filename
Showing non-printable characters with bat

Showing non-printable characters with bat

Conclusion

In conclusion, the ‘bat‘ command in Linux is a remarkable tool that enhances the text file viewing experience in the terminal. With its syntax highlighting, line numbering, and code folding capabilities, it offers a substantial improvement over the traditional ‘cat‘ command, particularly for developers and system administrators working with code and configuration files.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads