How to Highlight Thousands Groups of Large Numers in Vim

By admin
Posted on

2023-09-27
DATE

When viewing large numbers it can be helpful to separate

thousands
CARDINAL

or some other grouping. Some languages provide a way to do this natively (such as

1_000_000
CARDINAL

) but others do not, or sometimes you are editing a generated file that doesn’t do this. I wrote a highlight rule for Vim to highlight the groups to make these numbers easier to read.

match SpellRare /\d\{1,3}\ze\%(\d\{6}\)*\d\{3}\>/

Replace SpellRare with any highlight group of your choosing. In my editor it looks like this:


1 1 2 12 3 1
DATE

234 4 123 456 5 1 234 567 890 6 12 345 678 901 234

I can imagine it being slightly smarter like not highlighting a

4
CARDINAL

-digit number, but it is simple and doesn’t noticeably slow down my editor. I’ve tried this in Vim and

Neovim
GPE

, and it works well in both.