How to install and Use Bat Command in Linux

How to install and Use Bat Command in Linux

As a Linux user, you must have come across the command-line utility ‘cat‘ which is used to display the contents of a file. However, have you ever heard of the bat command? If not, you’re missing out on one of the most powerful and convenient tools that Linux has to offer.

In this comprehensive guide, we will take a deep dive into the ‘bat’ command, exploring its features, capabilities, and practical applications.

What is the ‘bat’ Command?

The ‘bat’ command is a modern alternative to the classic ‘cat’ command. It is a tool for viewing and paging through files in the terminal. The main advantage of ‘bat’ over ‘cat’ is that it provides syntax highlighting and other features that make it easier to read and understand the contents of files.

Features of the ‘bat’ command

The ‘bat’ command offers several features that make it a valuable tool for Linux users. These features include:

  1. Syntax highlighting: ‘bat’ highlights the syntax of different programming languages, making code more readable and understandable.
  2. Automatic paging: When working with large files, the ‘bat’ command automatically pages the output, making it easier to read and manage.
  3. Git integration: ‘bat’ can be used to view Git diffs and highlights the changes in syntax, making it easier to identify and understand modifications.
  4. Display and highlight non-printable characters.
  5. A user-friendly command-line interface
  6. File concatenation, including all of the cat command functionality.

How to Install the ‘bat’ Command in Linux

The ‘bat’ command is not pre-installed on most Linux systems, but it can be easily installed. If you’re using a Debian-based distribution, you can install it using the following command:

Install Using apt

sudo apt install bat

Important: If you install bat this way, please note that the executable may be installed as batcat instead of bat (due to a name clash with another package). You can set up a bat -> batcat symlink or alias to prevent any issues that may come up because of this and to be consistent with other distributions:

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

Install Using Most Recent .deb Packages

If the latest version of ‘bat’ is not available on your Ubuntu/Debian installation, or if you want to install the most recent release, you can download the latest .deb package from the official release page and install it on your system using command below:

sudo dpkg -i bat_0.18.3_amd64.deb  # adapt version number and architecture

For other distributions, you can check the official documentation of ‘bat’ to find the installation instructions.


Syntax of the ‘bat’ Command

The syntax of the ‘bat’ command is straightforward and easy to use. Here is an example:

bat [OPTIONS] [FILE(S)]

The ‘OPTIONS‘ argument is optional and can be used to specify various options such as the number of lines to display, line numbers, and others. The ‘FILE(S)‘ argument is mandatory and specifies the file(s) you want to view.

Some of the most commonly used options are:

  • -h or --help: Displays the help message
  • -l or --line-range: Displays a range of lines (e.g., -l 5-10)
  • -n or --line-number: Displays line numbers
  • -p or --plain: Displays the file without syntax highlighting
  • -r or --recurse: Displays all files in a directory recursively
  • -s or --style: Specifies the syntax highlighting style to use
  • -V or --version: Displays the version information

View Multiple Files Using ‘bat’ Command

The ‘bat’ command allows you to view multiple files at once by passing in a list of files as arguments. For example:

bat file1.txt file2.txt file3.txt

This will display the contents of all three files in the terminal, one after the other.


View Specific Lines Using ‘bat’ Command

The ‘-r‘ or ‘–range‘ option allows you to specify a range of lines to display. For example, to display lines 5-10 of a file, use the following command:

bat -r 5:10 file.txt

View Line Numbers Using ‘bat’ Command

The ‘-n‘ or ‘–number‘ option allows you to display line numbers next to each line. For example:

bat -n file.txt

Specify a Theme Using ‘bat’ Command

The ‘-T‘ or ‘–theme‘ option allows you to specify a theme for syntax highlighting. For example, to use the ‘TwoDark’ theme, use the following command:

bat -T TwoDark file.txt

'bat‘ command comes with a host of themes for syntax highlighting. You can get a list of them, along with samples, using the command below:

bat --list-themes

Syntax Highlighting using ‘bat’ Command

One of the most powerful features of the ‘bat‘ command is syntax highlighting. By default, ‘bat’ detects the file type and applies the appropriate syntax highlighting. However, you can also specify the language explicitly using the ‘-l‘ or ‘–language‘ option. For example:

bat -l python file.py

Piping Output Using ‘bat’ Command

The ‘bat’ command can be used in conjunction with other commands by piping their output to it. For example:

ls -l | bat

This will display the output of the ‘ls -l‘ command with syntax highlighting in the terminal.


Sorting Files by Size Using ‘bat’ Command

The ‘-S‘ or ‘–sort‘ option allows you to sort the files by size. For example:

bat -S file1.txt file2.txt file3.txt

This will display the contents of the files sorted by size.


Searching for Text Using ‘bat’ Command

The ‘bat‘ command allows you to search for text within a file using the ‘-G‘ or ‘–grep‘ option. For example, to search for the word ‘hello’ in a file, use the following command:

bat -G hello file.txt

Using ‘bat’ Command with Git

If you’re working with Git, you can use the ‘bat’ command to view the contents of files in the repository. For example, to view the contents of a file in the current branch, use the following command:

git show HEAD:file.txt | bat

Using bat Command with Vim

If you use Vim as your text editor, you can use the ‘bat’ command as a pager. For example, to set ‘bat‘ as the pager in Vim, add the following line to your .vimrc file:

set pager=bat

Comparing Files with bat Command

The ‘bat‘ command allows you to compare the contents of two files side by side using the ‘-d‘ or ‘–diff‘ option. For example:

bat -d file1.txt file2.txt

This will display the contents of the two files side by side, highlighting the differences between them.


Viewing Markdown Files with bat Command

If you frequently work with Markdown files, the ‘bat’ command can be a handy tool for viewing them. ‘bat’ automatically detects Markdown files and applies syntax highlighting to make them easier to read. For example:

bat README.md

Using bat Command in a Shell Script

The ‘bat’ command can be used in shell scripts to display output with syntax highlighting. For example:

echo "Hello world!" | bat

This will display the output of the ‘echo’ command with syntax highlighting.


Using ‘bat’ Command with Tmux

If you use Tmux as your terminal multiplexer, you can use the ‘bat’ command to display files in a split pane. For example, to split the current pane and display a file with ‘bat’, use the following command:

tmux split-window 'bat file.txt'

Conclusion:

The ‘bat’ command is a powerful tool for viewing and paging through files in the terminal. Its syntax highlighting and other features make it a valuable addition to any Linux user’s toolkit. With the examples provided in this guide, you should now be able to use the ‘bat’ command to its fullest potential. Whether you’re a developer, system administrator, or casual Linux user, ‘bat’ is a tool you won’t want to be without.

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top