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
2. Displaying multiple files with bat
To display multiple files at once, we can make use of regex in the command.
bat ./*.java
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
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
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
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
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"
Step 2: Enter into the bat config syntaxes directory.
cd "$(bat --config-dir)/syntaxes"
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"
Step 4: Let the bat command parse the syntax files
bat cache --build
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"
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"
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
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
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
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
7. Show non-printable characters.
Use the -A or –show-all flag for the non-printable characters.
bat --show-all filename
bat -A filename
Bat – A Cat Clone with Syntax Highlighting and Git Integration
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:
- 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.
- Line Numbers: It displays line numbers by default, helping users quickly locate and reference specific lines within a file.
- 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.
- Customizable Themes: Users can customize the color themes used for syntax highlighting to suit their preferences.
- 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.