What Is It?

I recently learned about the bat command available on Linux and wanted to share it in case it was useful to anyone else. If you’re like me and you find it easier to read log and config files when they have syntax highlighting bat might be for you, it’s a drop in replacement for cat and has pretty comprehensive syntax highlighting capabilities for anything from nginx logs to JSON and JavaScript files.

In addition to syntax highlighting bat also adds line numbers and has options for various additional features like running a git diff, choosing a specific line range or highlighting a particular line number.

Examples

Show only lines that have been added/removed/modified according to git:

bat -d app.js

Show only lines 20-30:

bat -r 20:30 app.js

Highlight line 28 in the output:

bat -H 28 app.js

Tailing / Watching A File

The one thing that’s missing from bat is a tail/watch option (something equivalent to tail -f) that’s not a complete surprise as bat is intended as a drop in replacement for cat which just echos files to standard output. However since this is a common use case when looking at log files you might also want to look at grc which is a generic colouriser for Linux.

The idea with grc is that you can still use the commands you’re familiar with like tail and cat but pass them into grc for syntax highlighting, it’s not as good as bat, it doesn’t seem to have support for JSON or JavaScript but works for log files.

grc tail -f /var/log/nginx/access.log

Sources

You’ll probably be able to find both in your package manager but in case you can’t or you want more info here are the links to the GitHub repositories:

BAT Github Repository

GRC Github Repository


0 Comments

Leave a Reply

Avatar placeholder

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.