You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sublime Text如何实现相似关键词的差异化颜色高亮?

在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:

  1. Open Preferences > Browse Packages to access your Sublime packages folder.
  2. Create a new file named CustomLang.sublime-syntax in the User subfolder.
  3. 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:

  1. Go to Preferences > Customize Color Scheme to open your user color scheme settings (a JSON file).
  2. 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:

  1. Create Packages/User/Python/Python.sublime-syntax
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:25:22