How to Highlight Thousands Groups of Large Numers in Vim
Tags: vim
Published: 2023-10-02 19:16:47

---
title: How to Highlight Thousands Groups of Large Numers in Vim
published: true
date: 2023-09-27 21:50:00 UTC
tags: Vim
canonical_url: https://kevincox.ca/2023/09/27/vim-thousands-highlight/
---

<main style="--width:min(36em,100%);display:grid;grid-template-columns:1fr var(--width) 1fr;overflow:auto;padding:0;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><p style="box-sizing:border-box;grid-column:2;margin:.4em">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 <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:0 .2em;white-space:pre-wrap">1_000_000</code>) 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.</p>
<div style="background:#222;border-color:#444;border-style:solid;border-width:1px 0;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;line-height:1;margin:.4em"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">match</span> SpellRare <span style="box-sizing:border-box;color:#b3d733;margin:0;padding:0">/\d\{1,3}\ze\%(\d\{6}\)*\d\{3}\&gt;/</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Replace <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:0 .2em;white-space:pre-wrap">SpellRare</code> with any highlight group of your choosing. In my editor it looks like this:</p>
<div style="background:#222;border-color:#444;border-style:solid;border-width:1px 0;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;line-height:1;margin:.4em"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="background-color:#080808;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:grey;margin:0;padding:0"> 1 </span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">1 </span></span>
<span style="background-color:#080808;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:grey;margin:0;padding:0"> 2 </span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">12 </span></span>
<span style="background-color:#080808;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:grey;margin:0;padding:0"> 3 </span></span><span style="background-color:#5f0000;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">1</span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">234 </span></span>
<span style="background-color:#080808;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:grey;margin:0;padding:0"> 4 </span></span><span style="background-color:#5f0000;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">123</span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">456 </span></span>
<span style="background-color:#080808;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:grey;margin:0;padding:0"> 5 </span></span><span style="background-color:#5f0000;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">1</span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">234</span></span><span style="background-color:#5f0000;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">567</span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">890 </span></span>
<span style="background-color:#080808;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:grey;margin:0;padding:0"> 6 </span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">12</span></span><span style="background-color:#5f0000;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">345</span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">678</span></span><span style="background-color:#5f0000;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">901</span></span><span style="background-color:#1c1c1c;box-sizing:border-box;margin:0;padding:0"><span style="box-sizing:border-box;color:#dadada;margin:0;padding:0">234 </span></span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">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.</p>
<h2 id="how-it-works" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/27/vim-thousands-highlight/#how-it-works" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">How it Works</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Basically it is matching a set of 1-3 digits that is followed by exactly <span style="box-sizing:border-box;margin:0;padding:0"><math xmlns="http://www.w3.org/1998/Math/MathML" style="box-sizing:border-box;margin:0"><semantics style="box-sizing:border-box;margin:0"><mrow style="box-sizing:border-box;margin:0"><mn style="box-sizing:border-box;margin:0">6</mn><mi style="box-sizing:border-box;margin:0">n</mi><mo style="box-sizing:border-box;margin:0">+</mo><mn style="box-sizing:border-box;margin:0">3</mn></mrow><annotation encoding="application/x-tex" style="box-sizing:border-box;margin:0">6n+3</annotation></semantics></math></span>digits then an end-of-word.</p></main>