Bat: How to Show Line Numbers

Updated

Learn how to show line numbers in a file using bat

Bat: How to Show Line Numbers

Before we start, make sure you have bat installed on your computer and the file you want to view ready in a folder. If you don't have bat yet, run this command:

brew install bat

First, open your terminal and navigate to the folder that has your file. When you run bat in a terminal, it shows line numbers by default, along with a header and a border:

bat input.py

Just remember to replace input.py with your actual filename. If you only want the numbers, without the header and the border, add the -n flag:

bat -n input.py

Bat only shows its decorations when it prints to a terminal, so if you pipe it into another command, the line numbers disappear. To keep them, add --decorations=always. Here I'm sending the result to grep to find every line with fizz in it:

bat --decorations=always input.py | grep fizz
undefined

One closing tip: if line numbers ever go missing because of your settings, add --style=+numbers and they come right back.