Emacs 之 Spacemacs 基础教程

· · 科技·工程

:::info[AI 使用说明] 本文在写作完成后使用 DeepSeek-R1 进行了润色。 :::

:::info[工具使用说明] 本文在写作完成后使用格式化工具进行了润色,仅对部分行修改。 :::

以下东西无法在考试时候使用,所以用于平常使用。

前言

Emacs 是个代码编辑器,初始配置简陋,但通过 Emacs Lisp 可以实现很强的可操作性和自定义。

而有些已经配置好的配置可以直接用,例如 Doom Emacs、Spacemacs,可以根据自己的操作习惯自己选择。这些会有更好的功能和外观。

Doom Emacs 和 Spacemacs 较为常用,他们都是用 Vim 键位。

Spacemacs 最大特点是:Spacemacs 还有自己的 SPC 键位同时兼容 Ctrl 的 Emacs 默认键位(看个人习惯)。

这里介绍 Spacemacs。

考场上没有网络,所以你就平常使用即可。

Spacemacs 使用 Vim 的键位和自己的 SPC 开头的快捷键,而不是 Alt/Option 和 Ctrl/Control,当然有 Emacs 键位当然也能用。

开启这篇教程前,你至少需要知道以下事物的基础用法:

接下来就可以食用这篇文章了。

安装

注意,你一定要已经安装过 Git,否则到 Spacemacs 安装部分会出问题!

Emacs 的部分不必多说,可以通过 GNU 官网安装 Emacs,官网链接点这里。

Emacs 的可操作性非常强,本身默认配置不怎么的所以使用第三方的配置(几千行代码你不会自己写吧)。

Windows 用户通过这里安装,或者点击安装包(Emacs 30)。

接下来就是 Spacemacs 的安装部分,请保持全程 GitHub 访问流畅。

Spacemacs 本身就作为配置,但是配置的完善,谁叫 Emacs 可自定义程度高呢

从 GitHub 克隆 Spacemacs,代码见下(或者 zip 解压):

Linux

git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d

Windows

需要看自己的配置目录在哪里,我这里是 ~/AppData/Roaming/(看不懂的看前言),也不排除直接是 ~/ 的可能性。

锁定到那个目录,然后:

git clone https://github.com/syl20bnr/spacemacs 

重命名文件夹为 .emacs.d 即可。

然后打开 Emacs,一定要是英文输入法!然后按下 v(使用 Vim 键位,如果使用其他键位亦可,本文使用 Vim 键位),然后按下 s(使用完整版的 Spacemacs,如果自己有能力可以按 b 使用 Spacemacs-base)。

然后等待安装完成即可。

如果出现报错,请重启 Emacs。每次重启的时候 Spacemacs 都会自动更新然后重新安装插件。

但请每次删除前,请删除安装包缓存,位于 Emacs 文件夹的 /.cache/quelpa/build/ 文件夹下,请选择性删除(全删也可以)。

经过与网络斗智斗勇,应当就可以安装成功了。

更改镜像源

如果网络实在不够流畅,可以考虑镜像源。

找到与 .emacs.d 所在文件夹下的 Spacemacs 配置文件 .spacemacs

找到 dotspacemacs/user-init 添加以下内容(以 develop 版本为例,具体教程看这里):

(setq configuration-layer-elpa-archives
      '(("melpa-cn" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
        ("org-cn"   . "https://mirrors.tuna.tsinghua.edu.cn/elpa/org/")
        ("gnu-cn"   . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")))

具体来说,可以总的修改为:

(defun dotspacemacs/user-init ()
  "Initialization for user code:
This function is called immediately after `dotspacemacs/init', before layer
configuration.
It is mostly for variables that should be set before packages are loaded.
If you are unsure, try setting them in `dotspacemacs/user-config' first."

  (setq configuration-layer-elpa-archives
      '(("melpa-cn" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
        ("org-cn"   . "https://mirrors.tuna.tsinghua.edu.cn/elpa/org/")
        ("gnu-cn"   . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")))
  )

配置

接下来,我们还需要一些配置。

C/C++

首先,确保你已经安装了 clangd,若是 Windows 用户还要将其添加至 Path,然后继续开始下一步。

不要使用 MSYS2 MSYS 架构或者 Cygwin 的 clangd,若不使用 MSVC 也不建议使用 Windows LLVM,这里使用 winlibs 自带的 clangd。

打开 .spacemacs 文件,如果你使用 Emacs 编辑,可以依次按下 SPC f e d

寻找 dotspacemacs-configuration-layers 在后面添加 (c-c++ :variables c-c++-backend 'lsp-clangd) 并解注释 lsp

最终结果类似于这样:

   ;; List of configuration layers to load.
   dotspacemacs-configuration-layers
   '(
     ;; ----------------------------------------------------------------
     ;; Example of useful layers you may want to use right away.
     ;; Uncomment some layer names and press `SPC f e R' (Vim style) or
     ;; `M-m f e R' (Emacs style) to install them.
     ;; ----------------------------------------------------------------
     ;; auto-completion
     ;; better-defaults
     emacs-lisp
     ;; git
     helm
     lsp
     ;; markdown
     multiple-cursors
     ;; org
     ;; (shell :variables
     ;;        shell-default-height 30
     ;;        shell-default-position 'bottom)
     ;; spell-checking
     ;; syntax-checking
     ;; version-control
     treemacs

     (c-c++ :variables c-c++-backend 'lsp-clangd))

重启 Emacs,它会自动安装 C++ 补全所需依赖项。

按下 SPC F F 打开一个文件试试。

进一步美化

字体字号

由于 Windows 没有 Source Code Pro,所以基本要改。

首先配置文件(.spacemacs)中找到 dotspacemacs-default-font 并自行修改。

例如我这里改成:

dotspacemacs-default-font '("Consolas"
                               :size 18.0
                               :weight normal
                               :width normal)

然后就似乎能用了?!所以这篇教程结束了。