强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

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 ProAdobe 出品github.com/adobe-fonts
IBM Plex MonoIBM 出装github.com/IBM/plex
Noto Sans MonoGoogle,多语言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 章:键位设计