主题

选择内置主题或定义自己的主题。

使用 OpenCode,你可以从多个内置主题中选择、使用适应终端主题的主题,或定义自己的自定义主题。

默认情况下,OpenCode 使用我们自己的 opencode 主题。


# 终端要求#

为了主题能够正确显示其完整的调色板,你的终端必须支持 truecolor(24 位颜色)。大多数现代终端默认支持此功能,但你可能需要启用它:

  • 检查支持:运行 echo $COLORTERM - 它应该输出 truecolor 或 24bit
  • 启用 truecolor:在 shell 配置文件中设置环境变量 COLORTERM=truecolor
  • 终端兼容性:确保你的终端模拟器支持 24 位颜色(大多数现代终端如 iTerm2、Alacritty、Kitty、Windows Terminal 和最新版本的 GNOME Terminal 都支持)

如果没有 truecolor 支持,主题可能会以降低的颜色准确度显示或回退到最接近的 256 色近似值。


# 内置主题#

OpenCode 带有多个内置主题。

名称描述
system适应终端的背景颜色
tokyonight基于 Tokyonight↗ 主题
everforest基于 Everforest↗ 主题
ayu基于 Ayu↗ 深色主题
catppuccin基于 Catppuccin↗ 主题
catppuccin-macchiato基于 Catppuccin↗ 主题
gruvbox基于 Gruvbox↗ 主题
kanagawa基于 Kanagawa↗ 主题
nord基于 Nord↗ 主题
matrix黑客风格的黑底绿字主题
one-dark基于 Atom One↗ Dark 主题

还有更多,我们正在不断添加新主题。


# 系统主题#

system 主题设计为自动适应终端的配色方案。与使用固定颜色的传统主题不同,system 主题:

  • 生成灰度:根据终端的背景颜色创建自定义灰度,确保最佳对比度。
  • 使用 ANSI 颜色:利用标准 ANSI 颜色(0-15)进行语法高亮和 UI 元素,这会尊重终端的调色板。
  • 保留终端默认值:对文本和背景颜色使用 none 以保持终端的原生外观。

系统主题适用于以下用户:

  • 希望 OpenCode 与终端外观匹配
  • 使用自定义终端配色方案
  • 更喜欢所有终端应用程序的一致外观

# 使用主题#

你可以通过使用 /theme 命令调出主题选择器来选择主题。或者你可以在配置中指定它。

json
1{
2  "$schema": "https://opencode.ai/config.json",
3  "theme": "tokyonight"
4}

# 自定义主题#

OpenCode 支持灵活的基于 JSON 的主题系统,允许用户轻松创建和自定义主题。


## 层次结构#

主题从多个目录按以下顺序加载,后面的目录会覆盖前面的:

  1. 内置主题 - 这些嵌入在二进制文件中
  2. 用户配置目录 - 定义在 ~/.config/opencode/themes/*.json 或 $XDG_CONFIG_HOME/opencode/themes/*.json
  3. 项目根目录 - 定义在 <project-root>/.opencode/themes/*.json
  4. 当前工作目录 - 定义在 ./.opencode/themes/*.json

如果多个目录包含同名主题,将使用优先级较高的目录中的主题。


## 创建主题#

要创建自定义主题,在其中一个主题目录中创建 JSON 文件。

对于用户范围的主题:

bash
mkdir -p ~/.config/opencode/themes
vim ~/.config/opencode/themes/my-theme.json

对于项目特定的主题:

bash
mkdir -p .opencode/themes
vim .opencode/themes/my-theme.json

## JSON 格式#

主题使用灵活的 JSON 格式,支持:

  • 十六进制颜色:"#ffffff"
  • ANSI 颜色:3(0-255)
  • 颜色引用:"primary" 或自定义定义
  • 深色/浅色变体:{"dark": "#000", "light": "#fff"}
  • 无颜色:"none" - 使用终端的默认颜色或透明

## 颜色定义#

defs 部分是可选的,它允许你定义可在主题中引用的可重用颜色。


## 终端默认值#

特殊值 "none" 可用于任何颜色以继承终端的默认颜色。这对于创建与终端配色方案无缝融合的主题特别有用:

  • "text": "none" - 使用终端的默认前景色
  • "background": "none" - 使用终端的默认背景色

## 示例#

以下是自定义主题的示例:

json
1{
2  "$schema": "https://opencode.ai/theme.json",
3  "defs": {
4    "nord0": "#2E3440",
5    "nord1": "#3B4252",
6    "nord2": "#434C5E",
7    "nord3": "#4C566A",
8    "nord4": "#D8DEE9",
9    "nord5": "#E5E9F0",
10    "nord6": "#ECEFF4",
11    "nord7": "#8FBCBB",
12    "nord8": "#88C0D0",
13    "nord9": "#81A1C1",
14    "nord10": "#5E81AC",
15    "nord11": "#BF616A",
16    "nord12": "#D08770",
17    "nord13": "#EBCB8B",
18    "nord14": "#A3BE8C",
19    "nord15": "#B48EAD"
20  },
21  "theme": {
22    "primary": {
23      "dark": "nord8",
24      "light": "nord10"
25    },
26    "secondary": {
27      "dark": "nord9",
28      "light": "nord9"
29    },
30    "accent": {
31      "dark": "nord7",
32      "light": "nord7"
33    },
34    "error": {
35      "dark": "nord11",
36      "light": "nord11"
37    },
38    "warning": {
39      "dark": "nord12",
40      "light": "nord12"
41    },
42    "success": {
43      "dark": "nord14",
44      "light": "nord14"
45    },
46    "info": {
47      "dark": "nord8",
48      "light": "nord10"
49    },
50    "text": {
51      "dark": "nord4",
52      "light": "nord0"
53    },
54    "textMuted": {
55      "dark": "nord3",
56      "light": "nord1"
57    },
58    "background": {
59      "dark": "nord0",
60      "light": "nord6"
61    },
62    "backgroundPanel": {
63      "dark": "nord1",
64      "light": "nord5"
65    },
66    "backgroundElement": {
67      "dark": "nord1",
68      "light": "nord4"
69    },
70    "border": {
71      "dark": "nord2",
72      "light": "nord3"
73    }
74  }
75}