bat
A command-line file viewer with syntax highlighting, Git-aware changes, line numbers, and automatic paging.
Why consider bat?
A clone of cat with built-in syntax highlighting and Git integration.
Learn bat by building
Practical setup notes, real use cases, and copy-ready examples in one focused guide.
In this guide5 sections
Upgrading Your Terminal Experience with bat
When working in the terminal, the traditional cat command is a fundamental tool for displaying file contents. However, for modern workflows, especially those involving code, it lacks visual cues that make reading text easier. The project bat addresses this gap by acting as a clone of cat with built-in syntax highlighting and Git integration.
This guide explores how to leverage bat to read files, visualize non-printable characters, and integrate the tool into broader command-line workflows.
Syntax Highlighting and Git Integration
The core appeal of bat is its ability to make code more readable directly in the terminal. When you view a file, bat automatically detects the language and applies appropriate colors to keywords, strings, and other language constructs. Furthermore, when reading from standard input, bat can determine the syntax automatically based on the first line of the file, such as a shebang.
In addition to syntax highlighting, bat uses version control data to show modifications with respect to the index. It displays a sidebar indicating lines that have been added, modified, or removed, similar to what you might see in a graphical code editor.
Displaying and Managing Output
To display a single file, invoke bat just as you would use cat:
bat README.mdIf the file you are viewing is long, bat automatically pipes its own output to a pager, ensuring you can scroll smoothly without the content overwhelming your terminal history. You can also explicitly instruct bat to always print to the screen without paging by using the --paging=never flag.
Despite the added visual features, bat remains fully compatible with standard terminal redirections. When the output is redirected to another process, bat will act as a drop-in replacement and fall back to printing the plain file contents. This means you can safely use bat in shell scripts to concatenate files without injecting ANSI color codes into your output file.
Inspecting Non-Printable Characters
Sometimes, especially when debugging configuration files or scripts, you need to see exactly what characters are present, including spaces, tabs, and line endings.
bat -A /etc/hostsBy providing the -A (or --show-all) flag, bat renders these invisible characters visible, allowing you to easily identify trailing spaces or mixed line-ending formats.
Integrating bat with Other Tools
The utility of bat extends beyond reading static files. You can combine it with other command-line utilities to enhance their output. For example, bat serves as an excellent previewer for fuzzy finders like fzf, providing a colorized preview of files as you search through directories. When integrating with other tools, you can restrict the load times for long files by specifying a line range option.
fzf --preview "bat --color=always --style=numbers --line-range=:500 {}"Conclusion
By adopting bat, you can significantly improve the clarity of code and data files viewed from the command line. Its reliable handling of syntax highlighting, Git modifications, and automatic paging makes it a substantial upgrade over traditional text display utilities.
Related tools
More options with a similar category or technology profile.
diskus
Minimal, fast alternative to du -sh written in Rust using multi-threaded directory traversal.
peco
Simplistic interactive filtering tool for Unix pipelines, process lists, and file trees.
Dapr CLI
Command-line tool for managing Dapr distributed application runtime environments and sidecars.
Freeze
Generate beautiful image screenshots and SVGs of code snippets and terminal outputs.