使用use-package整理Emacs的init.el时,custom-set-variables块该放何处?
关于Emacs init.el中custom-set-variables代码块的放置位置(配合use-package)
核心结论
把custom-set-variables和custom-set-faces代码块放在init.el的最末尾,也就是所有use-package配置代码的后面。
原因说明
- Emacs的Custom系统会自动维护这两个代码块,用来记录你通过图形界面或交互操作修改的配置。放在末尾能避免被前面的use-package配置覆盖,确保这些自定义设置最终生效。
- use-package是主动声明包的加载逻辑和配置,属于“前置定义”;而Custom块是“后续记录”的配置,两者顺序错开不会干扰彼此的加载优先级。
示例配置结构
;; 先配置use-package基础设置 (use-package use-package :ensure t :config (setq use-package-always-ensure t)) ;; 其他包的use-package配置示例 (use-package magit :bind ("C-x g" . magit-status)) (use-package which-key :config (which-key-mode)) ;; ... 更多use-package配置 ... ;; 最后放Custom自动生成的代码块 (custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. '(ispell-dictionary nil) '(package-selected-packages '(sonic-pi browse-kill-ring sclang-extensions async magit avy csv-mode company ein conda perspective buffer-move pdf-tools which-key helpful use-package sudo-edit)) '(send-mail-function 'mailclient-send-it)) (custom-set-faces ;; custom-set-faces was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. )
额外注意事项
- 尽量不要手动修改Custom块内的内容,所有主动配置优先通过use-package或
setq完成,让Custom块仅保留Emacs自动生成的记录。 - 如果想把
package-selected-packages里的包迁移到use-package,可以逐个用(use-package 包名 :ensure t)声明,之后Emacs会自动更新Custom块里的包列表。
内容的提问来源于stack exchange,提问作者d m
相关产品推荐
相关产品推荐

