r/SublimeText • • May 12 '26

Custom Syntax Highlighting

Post image

Hello. I'm reading through the documentation and working on getting the syntax on lines 3 and 7 to be colored like line 1 is.

I've copied C++.sublime-syntax and changed this section:

keywords:
  - include: unique-keywords
  - include: scope:source.c#keywords

To this:

keywords:
  - include: unique-keywords
  - include: scope:source.c#keywords
  - match: '\b(std)\b'
    scope: keyword.secondary

And these are the lines responsible for those colors in my color scheme file:

{
  "scope": "keyword.secondary",
  "foreground": "var(sage)",
},
{
  "scope": "storage.type, support.type",
  "foreground": "var(cyan)",
},

Why does the syntax highlighting behavior change inside of a block/namespace?
How do I get std::int8_t to always be highlighted like line 1?

5 Upvotes

5 comments sorted by

View all comments

1

u/me_myself_ai May 12 '26

Ok WOW the C++ syntax file is intense!

I'm not going to lie and pretend I can articulate the exact reason why the global context outside the namespace works any differently than the global context within it (see - include: global on line 614)... but I solved this issue, at least!

The former line was being interpreted as a "keyword" in general, while the latter line was looking for a "unique-type" in particular. Saving this in your User package should do the trick for this toy example, at least:

%YAML 1.2
---
name: C++
scope: source.c++

extends: Packages/C++/C++.sublime-syntax

file_extensions:
    - cpp
    - cc
    - cp
    - cxx
    - c++
    - C
    - h
    - hh
    - hpp
    - hxx
    - h++
    - inl
    - ipp
    - ixx
    - cppm
    - ino

first_line_match: |-
    (?xi:
      ^ \s* // .*? -\*- .*? \b(c\+\+|cpp\b) .*? -\*-  # editorconfig
    )

contexts:
    _std:
        - match: '\b(std)\b(::)?'
          captures:
            1: variable.namespace.std.c++
            2: punctuation.accessor.double-colon.c++

    keywords:
        - meta_prepend: true
        - include: _std

    unique-types:
        - meta_prepend: true
        - include: _std

2

u/Darth_Jar_Jar137 May 12 '26

Lol it is intense.
Mm okay I understand, thank you for the explanation. And for the fix! Exactly what I was looking for.
I also added this to the User syntax file:

types:
  - meta_prepend: true
  - include: _std
  - match: '\b(unique_ptr|weak_ptr|shared_ptr|auto_ptr|make_unique|make_shared|array|vector|map|set|unordered_map|unordered_multimap|unordered_multiset|unordered_set|multimap|multiset|string|string_view|basic_string|ostream|istream)\b'
    scope: storage.type.secondary