Posted on
Software

bat: Modern `cat` with syntax highlighting

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Unlock the Power of bat: A Modern Replacement for cat with Syntax Highlighting

If you frequently find yourself working in the Linux terminal, you're probably familiar with cat, a widely-used command for file concatenation and display. However, if you're a programmer or someone who deals with configuration files and logs, you might want something more visually informative, especially when it comes to syntax. Enter bat - a command line tool that serves as a drop-in replacement for cat but with the supercharged features of syntax highlighting, Git integration, and automatic paging.

What is bat?

bat is much like cat, but with syntax highlighting and a lot more features. It supports a wide range of programming and markup languages and integrates with Git to show modifications in the file. It makes reading and understanding code or configuration snippets easier directly from the command line. Plus, it automatically pipes its output through a pager (like less) if the content is too large to fit on one terminal screen.

Key Features of bat

  • Syntax highlighting supporting multiple file extensions

  • Integration with Git to show modifications directly

  • Automatic paging for large files

  • File concatenation capabilities similar to cat

  • Customizable themes for syntax highlighting

  • Support for displaying non-text files if possible (such as images or PDFs, through delegating to specifics tools)

Installation Instructions

Installing bat is straightforward. Below, you'll find the commands for the most common Linux distributions using their default package managers.

For Debian and Ubuntu-based distributions:

bat can be installed via apt:

sudo apt update
sudo apt install bat

If the package name bat conflicts or isn't available, try installing batcat instead, which is the same tool under a different name:

sudo apt install batcat

And you can create a symbolic link if you prefer using the bat command:

sudo ln -s /usr/bin/batcat /usr/local/bin/bat

For Fedora, Red Hat, and CentOS:

Use dnf package manager to install bat:

sudo dnf install bat

For openSUSE:

bat can be installed using zypper:

sudo zypper install bat

Using bat

Once installed, using bat is as simple as using cat. To view a file with bat, type:

bat filename

This will display the file with beautiful syntax highlighting. Additionally, bat also supports viewing multiple files at once:

bat file1.txt file2.txt file3.txt

And you can even combine it with other commands via piping:

ls | bat -p

This command will pipe the output of ls through bat, highlighting it accordingly (the -p flag makes sure the output is not automatically paged).

Customizing bat

bat is highly customizable. For example, you can change the color theme used for syntax highlighting. List all available themes with:

bat --list-themes

To set a theme, use the --theme option:

bat --theme=TwoDark filename

To make a change permanent, export the BAT_THEME environment variable in your shell configuration file (like .bashrc):

export BAT_THEME="TwoDark"

Conclusion

While cat remains a staple of the Linux toolkit, bat elevates the experience to a new level, especially for developers and system administrators. By visually distinguishing syntax and integrating seamlessly with other utilities, bat makes terminal-based file exploration both more efficient and enjoyable. Give bat a try, and you might find it indispensable in your command-line adventures. For more details, check out the official bat GitHub repository. Happy coding!