Emacs 完全指南 / 第 16 章:界面定制
第 16 章:界面定制
16.1 主题
主题对比
| 主题 | 风格 | 特点 |
|---|
| Doom One | 深色 | Doom Emacs 默认,类似 One Dark |
| Doom Dracula | 深色 | 经典紫色调 |
| Modus Vivendi | 深色 | 内置,WCAG AAA 无障碍标准 |
| Modus Operandi | 浅色 | 内置,极高对比度 |
| Catppuccin | 深/浅 | 柔和色调,Mocha/Macchiato/Frappe/Latte |
| Zenburn | 深色 | 低对比度护眼 |
| Gruvbox | 深/浅 | 复古暖色调 |
| Kanagawa | 深色 | 日式配色 |
| Ef Themes | 深/浅 | Prot 设计,科学配色 |
安装与切换
;; 安装主题包
(use-package doom-themes
:config
(load-theme 'doom-one t)
;; Doom 特性
(doom-themes-visual-bell-config)
(doom-themes-org-config))
;; 或者使用 ef-themes(Protesilaos 设计)
(use-package ef-themes
:config
(ef-themes-select 'ef-dark))
;; 切换主题快捷方式
(defun my/cycle-theme ()
"在常用主题间循环切换。"
(interactive)
(let* ((themes '(doom-one doom-dracula modus-vivendi modus-operandi))
(current (car custom-enabled-themes))
(next (or (cadr (memq current themes))
(car themes))))
(mapc #'disable-theme custom-enabled-themes)
(load-theme next t)
(message "主题切换到: %s" next)))
(global-set-key (kbd "C-c T") 'my/cycle-theme)
;; 使用 consult 快速切换
;; M-x consult-theme
亮暗模式自动切换
;; 根据系统主题自动切换
(defun my/apply-theme (appearance)
"根据 APPEARANCE 切换主题。"
(mapc #'disable-theme custom-enabled-themes)
(pcase appearance
('light (load-theme 'modus-operandi t))
('dark (load-theme 'modus-vivendi t))))
;; macOS 跟随系统(需要 Emacs 29+)
(add-hook 'ns-system-appearance-change-functions #'my/apply-theme)
;; Linux 桌面环境(需要 dbus)
;; 可以通过 dbus-monitor 监听系统主题变化
16.2 模型栏(Mode Line)
内置模型栏配置
;; 简化模型栏
(setq-default mode-line-format
'("%e"
mode-line-front-space
mode-line-modified
" "
mode-line-buffer-identification
" "
mode-line-position
" "
mode-line-modes
mode-line-misc-info
mode-line-end-spaces))
;; 显示列号
(column-number-mode 1)
;; 显示编码信息
(setq mode-line-misc-info
'(("" mode-line-mule-info)))
Doom Modeline(推荐)
(use-package doom-modeline
:hook (after-init . doom-modeline-mode)
:config
(setq doom-modeline-height 25
doom-modeline-bar-width 4
doom-modeline-buffer-file-name-style 'truncate-upto-project
doom-modeline-icon t
doom-modeline-major-mode-icon t
doom-modeline-major-mode-color-icon t
doom-modeline-buffer-state-icon t
doom-modeline-lsp t
doom-modeline-vcs-max-length 20
doom-modeline-env-version t))
;; 需要安装字体
;; M-x nerd-icons-install-fonts
Modeline 信息含义
┌───────────────────────────────────────────────────────┐
│ @ main ~/project/src/main.py Python LSP (12,45) │
│ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ └─ 行,列
│ │ │ │ │ │ └─ LSP 状态
│ │ │ │ │ └─ 主模式
│ │ │ │ └─ 已修改标记
│ │ │ └─ 文件路径
│ │ └─ Git 分支
│ └─ 工作空间/透视
└───────────────────────────────────────────────────────┘
16.3 图标
Nerd Icons
;; Nerd Icons(推荐,Emacs 29+ 生态主流)
(use-package nerd-icons)
;; Dired 中的图标
(use-package nerd-icons-dired
:hook (dired-mode . nerd-icons-dired-mode))
;; Ibuffer 中的图标
(use-package nerd-icons-ibuffer
:hook (ibuffer-mode . nerd-icons-ibuffer-mode))
;; 安装字体
;; M-x nerd-icons-install-fonts
;; 或手动下载:https://github.com/ryanoasis/nerd-fonts
All The Icons(旧方案)
;; all-the-icons(旧方案,建议使用 nerd-icons 替代)
(use-package all-the-icons
:if (display-graphic-p))
(use-package all-the-icons-dired
:hook (dired-mode . all-the-icons-dired-mode))
16.4 字体配置
推荐编程字体
| 字体 | 特点 | 链接 |
|---|
| JetBrains Mono | 现代,连字支持 | jetbrains.com |
| Fira Code | 连字,经典 | github.com/tonsky/FiraCode |
| Iosevka | 窄体,高密度 | typeof.net/Iosevka |
| Cascadia Code | 微软出品 | github.com/microsoft/cascadia-code |
| Source Code Pro | Adobe 出品 | github.com/adobe-fonts |
| IBM Plex Mono | IBM 出装 | github.com/IBM/plex |
| Noto Sans Mono | Google,多语言 | fonts.google.com |
| Sarasa Gothic | 更纱黑体,中英文等宽 | github.com/be5invis/Sarasa-Gothic |
字体配置
;; 基本字体配置
(set-face-attribute 'default nil
:family "JetBrains Mono"
:height 140) ; 14pt = 140
;; 中文字体
(set-fontset-font "fontset-default" 'chinese-gbk
(font-spec :family "Sarasa Gothic" :size 14))
(set-fontset-font t 'han "Sarasa Gothic")
;; 变量宽度字体(用于 Org-mode 等)
(set-face-attribute 'variable-pitch nil
:family "Noto Sans"
:height 140)
;; 字体缩放
(global-set-key (kbd "C-=") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)
(global-set-key (kbd "C-0") 'text-scale-adjust)
;; 使用 fontaine 管理字体预设
(use-package fontaine
:config
(setq fontaine-presets
'((small :default-height 120)
(regular :default-height 140)
(large :default-height 180)
(presentation :default-height 240)))
(fontaine-set-preset 'regular))
;; 连字支持
(use-package ligature
:config
(ligature-set-ligatures 'prog-mode '("->" "<-" "=>" "==" "!=" "<=" ">="
"&&" "||" "++" "--" "::" "..."
"!!" "??" "##" "/*" "*/"))
(global-ligature-mode t))
字体诊断
;; 检查当前字体
M-x describe-font RET RET
;; 查看所有字体
M-x list-fontsets
;; 查看字符对应的字体
C-u C-x = ; 将光标放在字符上执行
16.5 显示优化
行号与空白
;; 行号
(global-display-line-numbers-mode 1)
(setq display-line-numbers-type 'relative) ; 相对行号
;; 在某些模式中禁用行号
(dolist (mode '(org-mode-hook
term-mode-hook
eshell-mode-hook
vterm-mode-hook))
(add-hook mode (lambda () (display-line-numbers-mode -1))))
;; 高亮当前行
(global-hl-line-mode 1)
;; 空白字符显示
(use-package whitespace
:hook (prog-mode . whitespace-mode)
:config
(setq whitespace-style '(face tabs spaces trailing lines-tail
space-before-tab newline
indentation empty space-after-tab
space-mark tab-mark newline-mark)))
;; 彩虹分隔线
(use-package indent-guide
:hook (prog-mode . indent-guide-mode))
;; 或者使用 highlight-indent-guides
(use-package highlight-indent-guides
:hook (prog-mode . highlight-indent-guides-mode)
:config
(setq highlight-indent-guides-method 'character
highlight-indent-guides-character ?│
highlight-indent-guides-responsive 'top))
括号高亮
;; 彩虹括号
(use-package rainbow-delimiters
:hook (prog-mode . rainbow-delimiters-mode))
;; 括号匹配增强
(use-package paren
:config
(setq show-paren-delay 0
show-paren-style 'parenthesis)
(show-paren-mode 1))
;; 高亮匹配括号颜色
(use-package highlight-parentheses
:hook (prog-mode . highlight-parentheses-mode))
16.6 弹窗与缓冲区显示
;; popwin - 管理弹窗
(use-package popwin
:config
(popwin-mode 1)
;; 常用弹窗配置
(push '("*Warnings*" :position bottom :height 10) popwin:special-display-config)
(push '("*Compile-Log*" :position bottom :height 10) popwin:special-display-config)
(push '("*compilation*" :position bottom :height 15) popwin:special-display-config)
(push '("*Flycheck errors*" :position bottom :height 10) popwin:special-display-config))
16.7 Dashboard(启动页)
(use-package dashboard
:init
(setq initial-buffer-choice 'dashboard-open)
:config
(setq dashboard-banner-logo-title "Welcome to Emacs"
dashboard-startup-banner 'official
dashboard-center-content t
dashboard-items '((recents . 10)
(bookmarks . 5)
(projects . 5)
(agenda . 5)))
(dashboard-setup-startup-hook))
16.8 本章小结
| 功能 | 工具 | 说明 |
|---|
| 主题 | doom-themes / ef-themes | 配色方案 |
| 模型栏 | doom-modeline | 信息丰富的状态栏 |
| 图标 | nerd-icons | 文件类型图标 |
| 字体 | fontaine / ligature | 编程字体与连字 |
| 行号 | display-line-numbers | 相对行号 |
| 括号 | rainbow-delimiters | 彩虹括号 |
| 缩进 | highlight-indent-guides | 缩进指示线 |
| 启动页 | dashboard | 自定义欢迎页面 |
16.9 扩展阅读
← 上一章 第 15 章:终端模拟 | 下一章 → 第 17 章:键位设计