Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change plain text color by indentation in VSCode

I am interested in giving lectures and taking notes in simple text files within VSCode (since I already use it for coding).

However, for the life of me I cannot figure out how to change simple text color. What I want is to have one text color at one indentation depth. For instance,

Notes:
    a:
    b:

Notes would be a different color than a and b. I want this for clarity of bullet points. Is there a simple way?

Thanks.

like image 576
James Bellamy Avatar asked Sep 18 '25 13:09

James Bellamy


1 Answers

You can color lines with different indentation differently with an extension like Highlight. For instance I can get this:

demo of Highlight extension

with this code in settings.json:

"highlight.regexes": {

  "((?<!.))(\\w.*)": [
    {},
    {
      "color": "green",
      "fontWeight": "bold",
      // "outline": "1px solid #fff",
      "letterSpacing": "1px"
    }
  ],
  "((?<!.))(  )(\\w.*)": [
    {},
    {},
    {
      "color": "yellow",
    }
  ],
  "((?<!.))(    )(\\w.*)": [
    {},
    {},
    {
      "color": "red",
    }
  ]
}

My setup inserts 2 spaces per tab - I found that you need to use spaces in your regex, not something handier like (\\t\\t) or ( ){4} but instead must use ( ) - that is 4 spaces representing two tabs for me - for the regex to work in the extension.

For more styling options, see https://code.visualstudio.com/api/references/vscode-api#DecorationRenderOptions

The extension link shows how to restrict this to whichever filetype(s) you wish. Which I incorporated into the second batch of code below.

If you want only the bullet headings, like a-z or 1-9 to be colored and not the rest of the text on that line, that can be achieved in the regex too:

"highlight.regexes": {

  "((?<!.))(\\w.*)": { 
    "regexFlags": "gi", 
    // "filterLanguageRegex": "markdown", 
    "filterFileRegex": "Notes.*\\.txt", 
    "decorations": [
      {}, 
      { 
        "color": "green",
        "fontWeight": "bold",
        "letterSpacing": "1px",
        "textDecoration": "underline"
      },
    ]
  },


  "((?<!.))(  )([a-z1-9][:\\.])(.*)":  {
    "regexFlags": "gi", 
    // "filterLanguageRegex": "markdown", 
    "filterFileRegex": "Notes.*\\.txt", 
    "decorations": [
      {},
      {},
      {
        "color": "yellow",
      },
      {}
    ]
  },


  "((?<!.))(    )([a-z1-9][:\\.])(.*)": {
    "regexFlags": "gi", 
    // "filterLanguageRegex": "markdown", 
    "filterFileRegex": "Notes.*\\.txt", 
    "decorations": [
      {},
      {},
      {
        "color": "red",
      },
      {}
    ]
  }

[Restricted to files of the form Notes1.txt, Notes blsdfs .txt and similar.]

demo 2 of Highlight extension

Obviously, you can have as many levels of indentation as you want with more regex entries following the same pattern.

like image 130
Mark Avatar answered Sep 21 '25 03:09

Mark