LinuxCommandLibrary

bat

Display file contents with syntax highlighting

TLDR

Pretty print the contents of one or more files to stdout

$ bat [path/to/file1 path/to/file2 ...]
copy

Concatenate several files into the target file
$ bat [path/to/file1 path/to/file2 ...] > [path/to/target_file]
copy

Remove decorations and disable paging (--style plain can be replaced with -p, or both options with -pp)
$ bat --style plain --pager never [path/to/file]
copy

Highlight a specific line or a range of lines with a different background color
$ bat [[-H|--highlight-line]] [10|5:10|:10|10:|10:+5] [path/to/file]
copy

Show non-printable characters like space, tab or newline
$ bat [[-A|--show-all]] [path/to/file]
copy

Remove all decorations except line numbers in the output
$ bat [[-n|--number]] [path/to/file]
copy

Syntax highlight a JSON file by explicitly setting the language
$ bat [[-l|--language]] json [path/to/file.json]
copy

Display all supported languages
$ bat [[-L|--list-languages]]
copy

SYNOPSIS

bat [OPTIONS] [FILE...]
bat [OPTIONS] --highlight-line <N>[:<N>] [...] [FILE...]
bat [OPTIONS] --list-themes
bat [OPTIONS] --list-languages
command | bat [OPTIONS]

PARAMETERS

-A, --show-all
    Show non-printable characters (tabs, spaces, newlines).

--diff
    Show Git diff markers (added, modified, removed lines).

-p, --plain
    Do not show Git modifications, line numbers, or automatic paging. Useful for scripting.

-P, --no-paging
    Do not pipe output to a pager, even if content exceeds terminal height. Useful when output needs to be directly captured.

--language <LANG>
    Explicitly set the language for syntax highlighting (e.g., 'Python', 'JSON').

--theme <THEME>
    Set the syntax highlighting theme (e.g., 'TwoDark', 'ansi'). Use --list-themes to see available options.

--list-themes
    List all available syntax highlighting themes.

--list-languages
    List all supported programming languages and file formats.

--style <COMPONENTS>
    Specify which elements to display (e.g., 'numbers,changes,header').

--wrap <MODE>
    Specify text wrapping mode: 'auto', 'never', or 'character'.

--line-range <N>[:<M>]
    Only show a specific range of lines from the input file(s).

--highlight-line <N>[:<M>]
    Highlight specific line numbers or ranges within the output.

DESCRIPTION

bat is a modern, feature-rich alternative to the standard cat command on Linux and Unix-like systems. Written in Rust, it enhances file viewing by providing significant improvements, primarily focusing on readability and developer experience.

Key features include: Syntax Highlighting: Automatically highlights code based on file type, supporting a vast array of programming languages and markup formats.
Git Integration: Shows Git modifications (added, modified, removed lines) directly within the output, similar to git diff.
Line Numbers: Displays line numbers by default, making navigation and referencing easier.
Automatic Paging: Pipes output to a pager like less automatically when content exceeds terminal height, allowing scrolling.
Configurability: Supports custom themes, aliases, and extensions through a configuration file.
Concise Output: Aims to provide a clean, readable output for various text files.

bat is designed to be a drop-in replacement for everyday cat usage, offering a more pleasant and informative viewing experience, especially for source code and structured text files.

CAVEATS

While bat is a powerful tool, it's not always a drop-in replacement for all cat use cases.
Scripting: For shell scripts where raw, unformatted text is strictly required (e.g., piping to grep for exact matches, or binary files), cat or bat --plain might be more suitable. bat's added formatting can interfere with automated parsing.
Performance: For extremely large files (gigabytes), bat might be slightly slower than cat due to the overhead of syntax parsing and formatting.
Dependencies: Relies on an external pager (like less) for automatic paging, which might not be available in minimal environments.

CONFIGURATION

bat can be extensively configured via a configuration file, typically located at $HOME/.config/bat/config or $XDG_CONFIG_HOME/bat/config. This file allows setting default options (e.g., preferred theme, paging behavior, line wrapping) and defining custom aliases or commands.

ENVIRONMENT VARIABLES

Several environment variables can influence bat's behavior, such as BAT_CONFIG_PATH to specify a custom configuration file location, BAT_PAGER to override the default pager (e.g., `more`), or BAT_THEME to set a default theme.

HISTORY

bat was created by sharkdp (Daniel Kehl) and first released around 2018. It is written entirely in Rust, a programming language known for its performance and memory safety. The project quickly gained significant traction within the developer community as a modern, user-friendly alternative to the traditional cat utility, addressing common pain points like lack of syntax highlighting and difficult navigation in large files. Its emphasis on a clean, readable output and integration with version control systems cemented its popularity as a go-to tool for viewing code and text files.

SEE ALSO

cat(1), less(1), more(1), grep(1), awk(1), sed(1)

Copied to clipboard