bat is a helpful alternative to cat when you want peruse files on the command-line with the improved readability that syntax highlighting has to offer. The Rust program boasts a wide selection of color themes, automatically uses the system pager when needed, provides syntax highlighting for man pages, and even provides basic integration with git. It is a wonderful tool, though it won’t replace your general pager, like vimpager, or your git pager, like delta.[1]

Tutorial

This tutorial walks through installing and configuring bat, version 0.16.0 at the time of writing, on an amd64 system running Ubuntu 20.04. It assumes you are familiar with common command-line utilities on Ubuntu and the less pager.

  1. Download the deb file for the latest version of bat for your particular computer’s CPU architecture.

    Releases can be found here. The following one-liner downloads the amd64 deb installer for the latest release.

    ➜ wget -q -nv -O - https://api.github.com/repos/sharkdp/bat/releases/latest \
       | awk -F': ' '/browser_download_url/ && /_amd64\.deb/ && !/musl/ {gsub(/"/, "", $(NF)); system("wget -qi -L " $(NF))}'
  2. Install the bat deb package.

    sudo apt install -y ./bat_*_amd64.deb
  3. Clean up the remaining deb package since it is no longer needed.

    rm bat_*_amd64.deb
  4. Create an initial configuration file.

    ➜ bat --generate-config-file
    Success! Config file written to /home/jordan/.config/bat/config
  5. Set the default color theme.

    ~/.config/bat/config
    --theme="Solarized (dark)"

    This sets the theme to Solarized Dark, my favorite color scheme.

  6. Highlight syntax with bat when using less.

    ~/.pam_environment
    LESS DEFAULT="-R"
    LESSOPEN DEFAULT="| bat --color always %s"
    You will have to log out and back in again for changes in ~/.pam_environment to take effect.
  7. Alias cat to bat because old habits die hard.

    alias -s cat (which bat)
  8. Highlight syntax within man pages.

    ~/.pam_environment
    MANPAGER DEFAULT="sh -c 'col -bx | bat -l man -p'"
  9. Behold that beautiful syntax highlighting.

    ➜ man 2 select

Conclusion

Reading code from the command-line should be a little bit more pleasant now. Enjoy!


1. According to this issue, bat might just be it’s own pager before long.