Sublime Text如何实现相似关键词的差异化颜色高亮?
Absolutely! You can definitely achieve differentiated color highlighting for similar keywords in Sublime Text using .sublime-syntax (YAML 1.2) files—it's the most robust and flexible approach for this. Let me walk you through a concrete, easy-to-follow example.
Step 1: Define Custom Scopes with a .sublime-syntax File
Scopes are unique identifiers Sublime uses to apply styling. We’ll create custom scopes for each similar keyword, then map those scopes to distinct colors later.
Let’s say we want to highlight three function-related keywords—func, function, and fn—with different colors. Here’s how to set it up:
- Open
Preferences > Browse Packagesto access your Sublime packages folder. - Create a new file named
CustomLang.sublime-syntaxin theUsersubfolder. - Paste this YAML code:
%YAML 1.2 --- name: CustomLang file_extensions: [cl] # Replace with your target file extension (e.g., py for Python) scope: source.customlang contexts: main: # Match "func" and assign a unique scope - match: '\bfunc\b' scope: keyword.control.customlang.func # Match "function" with a different scope - match: '\bfunction\b' scope: keyword.control.customlang.function # Match "fn" with a third unique scope - match: '\bfn\b' scope: keyword.control.customlang.fn
The \b ensures we only match whole words, so we don’t accidentally highlight parts of longer terms (like "functionality").
Step 2: Map Scopes to Colors in Your Color Scheme
Now we’ll tell Sublime what color to use for each custom scope:
- Go to
Preferences > Customize Color Schemeto open your user color scheme settings (a JSON file). - Add these rules to the
"rules"array (adjust the hex codes to your preferred colors):
{ "rules": [ { "scope": "keyword.control.customlang.func", "foreground": "#FF5733" // Bright orange }, { "scope": "keyword.control.customlang.function", "foreground": "#33FF57" // Lime green }, { "scope": "keyword.control.customlang.fn", "foreground": "#3357FF" // Soft blue } ] }
Save the file, and any file with your custom extension (.cl in this example) will now display each keyword in its unique color.
Extending an Existing Language (e.g., Python)
If you want to modify an existing syntax (like Python) instead of creating a new one, you can extend its default syntax file:
- Create
Packages/User/Python/Python.sublime-syntax - Paste this code to add custom keyword highlighting while preserving default Python syntax:
%YAML 1.2 --- extends: Packages/Python/Python.sublime-syntax contexts: main: # Add your custom keyword match first - match: '\bmy_special_func\b' scope: keyword.control.python.specialfunc # Include the original Python syntax rules - include: main@super
Then add the corresponding color rule in your color scheme as before.
Alternative: Temporary Highlighting
For one-off, non-permanent highlighting, you can use Sublime’s built-in "Mark" feature (Ctrl+Shift+M on Windows/Linux, Cmd+Shift+M on Mac) or install the Highlight Words plugin via Package Control. But for permanent, syntax-aware styling, .sublime-syntax is the best solution.
内容的提问来源于stack exchange,提问作者FMarkus

