新手第一次用doom配置emacs

没错,我今天一天(假期的最后一天)都在折腾这个配置文件。先在此祝大家开工大吉吧。

新手入门,有什么不对的,或者更适合的建议,还请多指教。 纯新的新手,连elisp语法都只看了5分钟的那种。 emacs-china 账号也是刚注册,还是热乎的 :smiling_imp:

elisp 一直是阻止我使用emacs的一大原因。从心理上是拒绝的。

只到某一天我试用了一下spacemacs的配置,打开了新大陆。

但是很多人也提到了, spacemacs 版本发布很慢,很多年没有发版(虽然develop分支一直有更新)。

相比之下,doom 的作者回复问题很勤快,维护也很积极。

于是我还是选择了 doom. 尽管这对于我这个新手来说,可能是不小的挑战。

首先,是terminal下使用无法显示256color的问题。

❯ emacs --version
GNU Emacs 27.1
Copyright (C) 2020 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

通过查询官方文档 Colors on a TTY (GNU Emacs FAQ) 设置了一个alias 解决。

alias em='TERM=xterm-direct emacs -nw'

原来 emacs (v27.1) 会检测 terminfo里的 setabsetaf , 与通常其它程序(比如neovim/vim或tmux检测TERM 名称是否包含color 或 RGB 不同)

绝大多数终端的TERM 都是 xterm-256color 或者与之兼容的, 但是 xterm-256color 的描述里并不会有 setabsetaf ,所以, emacs 这一行为有些与众不同了。这一点确实让我折腾了很久。

spacemacs wiki 有专门说明这个问题,但对于现行的大部分Linux发行版而方,根本不必要手动生成一个terminfo, 使用默认的xterm-direct 即可 。

其次是 lsp 默认不启用,得手动执行 :+lsp 启动 lang server, 这个问题在使用 spacemacs 时完全不会遇到。

经过查询文档https://emacs-lsp.github.io/lsp-mode/page/settings/ 找到一个名为lsp-auto-guess-root 的配置

修改 config.el 增加如下配置解决:

(after! lsp-mode
(setq lsp-auto-guess-root t))

另外, Ivy 的仓库不叫Ivy,而是 swiper 这有点令我震惊。

emacs 在 doom 下加载了232个包时,启动时间确实比较慢,虽然我机器配置也不差了。

Doom loaded 232 packages across 45 modules in 0.467s

最后,我的配置:

启用 evil , 然后把默认的leader key 从 spc 修改成了 ,

config.el

;;; $DOOMDIR/config.el -*- lexical-binding: t; -*-

;; Place your private configuration here! Remember, you do not need to run 'doom
;; sync' after modifying this file!


;; TERM=xterm-direct emacs -nw
;; fixup 24bit color for tmux when TERM=screen-256color
;; 下面的好像没啥用? 所以我注释掉了
;; 我添加下面这些只是想不再需要 TERM=xterm-direct emacs -nw 这个alias来启动

;; (add-to-list 'term-file-aliases
;;    '("screen-256color" ."xterm-direct"))

;;(add-to-list 'term-file-aliases
;;    '("tmux-256color" ."xterm-direct"))

;;(add-to-list 'term-file-aliases
;;    '("xterm-256color" ."xterm-direct"))

(setq evil-snipe-override-evil-repeat-keys nil)
(setq doom-leader-key ",")
(setq doom-localleader-key ",")

(setq user-full-name "用户名"
      user-mail-address "邮箱地址")


 (setq doom-font (font-spec :family "JetBrainsMono Nerd Font" :size 12 :weight 'semi-light)
       doom-variable-pitch-font (font-spec :family "Noto Sans CJK SC" :size 13))

(if (equal (display-pixel-width) 3840)
   (setq doom-font (font-spec :family "JetBrainsMono Nerd Font" :size 28 :weight 'semi-light)
       doom-variable-pitch-font (font-spec :family "Noto Sans CJK SC" :size 28)))

(setq doom-theme 'doom-material)

(setq org-directory "~/org/")


(setq display-line-numbers-type t)

;; doom 内置包配置
(after! lsp-mode
(setq lsp-log-io t)
(setq lsp-auto-guess-root t)
)

(after! magit
(setq git-commit-fill-column 120))

;; enable responsive guides
;; responsive guides allow you to visualize not only the indentation itself, but your place in it.
;; see https://github.com/DarthFennec/highlight-indent-guides/blob/master/README.md#responsive-guides
;; can be nil (default) / top / stack
(after! highlight-indent-guides
  (setq highlight-indent-guides-responsive "top"))

;; emacs 内置功能配置
;; enable fill-column-indicator (since emacs 27.1) in prog mode
;; see https://www.gnu.org/software/emacs/manual/html_node/emacs/Displaying-Boundaries.html
(add-hook 'prog-mode-hook (lambda ()
        (setq fill-column 120)
        (setq display-fill-column-indicator t)
        (setq display-fill-column-indicator-column t)
        ;; (display-fill-column-indicator-mode)
        (global-display-fill-column-indicator-mode)
))


;; user packages config

(use-package! rainbow-mode
  :config
  (progn
    (defun @-enable-rainbow ()
      (rainbow-mode t))
    (add-hook 'prog-mode-hook '@-enable-rainbow)
))

(use-package! rainbow-delimiters
  :config
  (progn
    (defun @-enable-rainbow-delimiters ()
      (rainbow-delimiters-mode t))
    (add-hook 'prog-mode-hook '@-enable-rainbow-delimiters)))

packages.el

;; -*- no-byte-compile: t; -*-
;;; $DOOMDIR/packages.el

(package! rainbow-mode)
(package! rainbow-delimiters)

init.el

;;; init.el -*- lexical-binding: t; -*-

;; This file controls what Doom modules are enabled and what order they load
;; in. Remember to run 'doom sync' after modifying it!

;; straight是直接从github仓库直接下载源码到本地然后编译的,
;; 所以emacs-china等二进制包的镜像根本不起作用
;;
;; see https://emacs-china.org/t/doom-emacs-straight-el/10383/18
;; and https://emacs-china.org/t/doom-emacs-straight-el/10383/8
;; doom这样做的原因在它源码里有详细的注释:
;; see https://github.com/hlissner/doom-emacs/blob/28b3ad452979669abbb1a2903dfaa51e304765b5/core/core-packages.el#L3
;;
;; 下面的配置只针对emacs内置的package.el起使用
;; doom 默认是禁用了package.el的,see
;; https://github.com/hlissner/doom-emacs/blob/28b3ad452979669abbb1a2903dfaa51e304765b5/core/core-packages.el#L55
;;
;; (setq package-archives
;;     '(("melpa-cn" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
;;       ("org-cn"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/org/")
;;       ("gnu-cn"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")))

(doom! :input
       ;;chinese
       ;;japanese
       ;;layout            ; auie,ctsrnm is the superior home row

       :completion
       (company +childframe)          ; the ultimate code completion backend
       ;;helm              ; the *other* search engine for love and life
       ;;ido               ; the other *other* search engine...
       (ivy +fuzzy +childframe +icons)               ; a search engine for love and life

       :ui
       ;;deft              ; notational velocity for Emacs
       doom              ; what makes DOOM look the way it does
       doom-dashboard    ; a nifty splash screen for Emacs
       doom-quit         ; DOOM quit-message prompts when you quit Emacs
       ;; (emoji +unicode)  ; 🙂
       ;; fill-column       ; a `fill-column' indicator
       hl-todo           ; highlight TODO/FIXME/NOTE/DEPRECATED/HACK/REVIEW
       ;;hydra
       indent-guides     ; highlighted indent columns
       ;;ligatures         ; ligatures and symbols to make your code pretty again
       minimap           ; show a map of the code on the side
       modeline          ; snazzy, Atom-inspired modeline, plus API
       ;;nav-flash         ; blink cursor line after big motions
       neotree           ; a project drawer, like NERDTree for vim
       ophints           ; highlight the region an operation acts on
       (popup +all +defaults)   ; tame sudden yet inevitable temporary windows
       ;;tabs              ; a tab bar for Emacs
       ;; treemacs          ; a project drawer, like neotree but cooler
       ;;unicode           ; extended unicode support for various languages
       vc-gutter         ; vcs diff in the fringe
       vi-tilde-fringe   ; fringe tildes to mark beyond EOB
       ;;window-select     ; visually switch windows
       workspaces        ; tab emulation, persistence & separate workspaces
       ;;zen               ; distraction-free coding or writing

       :editor
       (evil +everywhere); come to the dark side, we have cookies
       file-templates    ; auto-snippets for empty files
       fold              ; (nigh) universal code folding
       (format +onsave)  ; automated prettiness
       ;;god               ; run Emacs commands without modifier keys
       ;;lispy             ; vim for lisp, for people who don't like vim
       multiple-cursors  ; editing in many places at once
       ;;objed             ; text object editing for the innocent
       ;;parinfer          ; turn lisp into python, sort of
       ;;rotate-text       ; cycle region at point between text candidates
       snippets          ; my elves. They type so I don't have to
       ;;word-wrap         ; soft wrapping with language-aware indent

       :emacs
       dired             ; making dired pretty [functional]
       electric          ; smarter, keyword-based electric-indent
       ;;ibuffer         ; interactive buffer management
       undo              ; persistent, smarter undo for your inevitable mistakes
       vc                ; version-control and Emacs, sitting in a tree

       :term
       ;;eshell            ; the elisp shell that works everywhere
       ;;shell             ; simple shell REPL for Emacs
       ;;term              ; basic terminal emulator for Emacs
       vterm             ; the best terminal emulation in Emacs

       :checkers
       syntax              ; tasing you for every semicolon you forget
       ;;(spell +flyspell) ; tasing you for misspelling mispelling
       ;;grammar           ; tasing grammar mistake every you make

       :tools
       ;;ansible
       ;;debugger          ; FIXME stepping through code, to help you add bugs
       ;;direnv
       ;;docker
       ;;editorconfig      ; let someone else argue about tabs vs spaces
       ;;ein               ; tame Jupyter notebooks with emacs
       (eval +overlay)     ; run code, run (also, repls)
       ;;gist              ; interacting with github gists
       lookup              ; navigate your code and its documentation
       (lsp +peek)
       magit             ; a git porcelain for Emacs
       make              ; run make tasks from Emacs
       ;;pass              ; password manager for nerds
       ;;pdf               ; pdf enhancements
       ;;prodigy           ; FIXME managing external services & code builders
       ;;rgb               ; creating color strings
       ;;taskrunner        ; taskrunner for all your projects
       ;;terraform         ; infrastructure as code
       ;;tmux              ; an API for interacting with tmux
       ;;upload            ; map local to remote projects via ssh/ftp

       :os
       (:if IS-MAC macos)  ; improve compatibility with macOS
       ;;tty               ; improve the terminal Emacs experience

       :lang
       ;;agda              ; types of types of types of types...
       cc                ; C/C++/Obj-C madness
       ;;clojure           ; java with a lisp
       ;;common-lisp       ; if you've seen one lisp, you've seen them all
       ;;coq               ; proofs-as-programs
       ;;crystal           ; ruby at the speed of c
       ;;csharp            ; unity, .NET, and mono shenanigans
       ;;data              ; config/data formats
       ;;(dart +flutter)   ; paint ui and not much else
       ;;elixir            ; erlang done right
       ;;elm               ; care for a cup of TEA?
       emacs-lisp        ; drown in parentheses
       ;;erlang            ; an elegant language for a more civilized age
       ;;ess               ; emacs speaks statistics
       ;;faust             ; dsp, but you get to keep your soul
       ;;fsharp            ; ML stands for Microsoft's Language
       ;;fstar             ; (dependent) types and (monadic) effects and Z3
       ;;gdscript          ; the language you waited for
       (go +lsp)         ; the hipster dialect
       ;;(haskell +dante)  ; a language that's lazier than I am
       ;;hy                ; readability of scheme w/ speed of python
       ;;idris             ; a language you can depend on
       json              ; At least it ain't XML
       ;;(java +meghanada) ; the poster child for carpal tunnel syndrome
       javascript        ; all(hope(abandon(ye(who(enter(here))))))
       ;;julia             ; a better, faster MATLAB
       ;;kotlin            ; a better, slicker Java(Script)
       ;;latex             ; writing papers in Emacs has never been so fun
       ;;lean
       ;;factor
       ;;ledger            ; an accounting system in Emacs
       lua               ; one-based indices? one-based indices
       markdown          ; writing docs for people to ignore
       ;;nim               ; python + lisp at the speed of c
       ;;nix               ; I hereby declare "nix geht mehr!"
       ;;ocaml             ; an objective camel
       org               ; organize your plain life in plain text
       (php +lsp)              ; perl's insecure younger brother
       ;;plantuml          ; diagrams for confusing people more
       ;;purescript        ; javascript, but functional
       ;;python            ; beautiful is better than ugly
       ;;qt                ; the 'cutest' gui framework ever
       ;;racket            ; a DSL for DSLs
       ;;raku              ; the artist formerly known as perl6
       ;;rest              ; Emacs as a REST client
       ;;rst               ; ReST in peace
       ;;(ruby +rails)     ; 1.step {|i| p "Ruby is #{i.even? ? 'love' : 'life'}"}
       (rust +lsp)              ; Fe2O3.unwrap().unwrap().unwrap().unwrap()
       ;;scala             ; java, but good
       ;;scheme            ; a fully conniving family of lisps
       sh                ; she sells {ba,z,fi}sh shells on the C xor
       ;;sml
       ;;solidity          ; do you need a blockchain? No.
       ;;swift             ; who asked for emoji variables?
       ;;terra             ; Earth and Moon in alignment for performance.
       ;;web               ; the tubes
       yaml              ; JSON, but readable

       :email
       ;;(mu4e +gmail)
       ;;notmuch
       ;;(wanderlust +gmail)

       :app
       ;;calendar
       ;;everywhere        ; *leave* Emacs!? You must be joking
       ;;irc               ; how neckbeards socialize
       ;;(rss +org)        ; emacs as an RSS reader
       ;;twitter           ; twitter client https://twitter.com/vnought

       :config
       ;;literate
       (default +bindings +smartparens))
3 个赞

等你某天发现 doom 的某些地方很慢,但是却死活找不到原因的时候。。。。。

1 个赞

某些地方 是哪些地方 :joy:

我不是 doom 用户,我只知道就是在 org-mode 里面,有时会特别慢。有拿完全相同的内容进行过测试。

2 个赞

doom用了大量lazy load,不是很喜欢这种做法,,虽然加快了启动时间,但是可能会导致启动后初次使用卡顿。。。

在org-mode里使用滚轮 :astonished:

1 个赞

我主要使用prog mode, 所以这个应该问题不大 :joy:

楼主,我比较推荐陈斌的配置,GitHub - redguardtoo/emacs.d: Fast and robust Emacs setup.

2 个赞

借楼问下,最近有没有人遇到 Doom 启动时大量 GC 导致启动速度很慢的。