How to Highlight Thousands Groups of Large Numers in Vim


When viewing large numbers it can be helpful to separate thousands or some other grouping. Some languages provide a way to do this natively (such as 1_000_000) 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 1234                
  4 123456              
  5 1234567890          
  6 12345678901234      

I can imagine it being slightly smarter like not highlighting a 4-digit number, but it is simple and doesn’t noticeably slow down my editor. I’ve tried this in Vim and Neovim, and it works well in both.

How it Works

Basically it is matching a set of 1-3 digits that is followed by exactly 6n+36n+3 digits then an end-of-word.