入门使用Spacemace(一款基于Emacs生态的配置框架软件)两周有余,以完全替代了我在编写Go/Python代码时使用的VSCode(另一款由Microsoft推出的开源IDE)。Spacemacs对生态插件集成、快捷键绑定有巨大的优势,全键盘操作可完成代码项目管理、编码、调试,熟悉后越用就越觉得这项技艺的珍贵,值得花时间研究。
因为是同事xingluyi带我入门,大大缩减了我作为初学者摸索、试错的时间,故从零到完全依赖,只花了2天。我将这期间的学习路径整理成文,方便后来者学习上手,也欢迎有经验者批评斧正。
安装到启动
首先要在系统上安装Emacs编辑器,由于我本身熟悉黑屏操作,选择源码安装。版本为27.2(目前最新版28.1,我选次新版27.2以回避Bug)。
# build from source
git clone https://github.com/emacs-mirror/emacs.git emacs-source
cd emacs-source && git checkout emacs-27.2
# add or remove options according to your needs
./configure --with-native-compilation --prefix=/usr/local/emacs \
--enable-profiling --with-json --with-imagemagick \
--with-pdumper=yes --without-x --with-pop --with-mailutils
随后安装Spacemacs配置框架,它有详细的安装文档,就按文档一步步操作。一直到首次启动Emacs,启动时会出现Spacemacs的Logo和可选项,那么安装就完成了。
常见的配置
安装成功只是完成了一半,此时Emacs可供作为一个普通的编辑器使用,不过也仅此而已。作为开发者,Spacemacs提供了诸多插件(Layer),在此我整理一些常见的插件及配置。
Markdown
开发者经常使用Markdown作为默认的文档文件格式,因此Markdown插件时必须安装的。在Markdown Layer文档有详细步骤,我们重点探讨预览功能。
提供预览功能的三方软件有3个,分别是markdown
, pandoc
, markdown_py
,我选择直接下载二进制的pandoc
以避免与其他语言(Python)仓库产生依赖。下载以后,在.spacemacs中作如下配置:
dotspacemacs-configuration-layers
'(
(markdown :variables
markdown-command "pandoc")
)
在Emacs通过SPC f e R
组合键热重载配置生效后,可通过SPC SPC markdown-mode
进入Markdown模式,通过SPC m c p
打开浏览器预览。因为是打开浏览器预览,这里要求运行环境必须是有图形桌面和浏览器的。
Golang
Golang是一门广受欢迎的开发语言,Spacemacs在Golang语言支持上已较成熟。这里配置主要注意2个方面:开发自动化和调试(debug)自动化。Golang Layer文档有详细步骤,在此重点关注自动补全、语法检查、调试,配置如下:
dotspacemacs-configuration-layers
'(
dap
(go :variables
go-tab-width 2
go-use-golangci-lint nil
go-format-before-save t
gofmt-command "goimports"
go-use-test-args "-race -timeout 10s"
godoc-at-point-function 'godoc-gogetdoc
go-backend 'lsp
)
)
这里经常使用的快捷键有,跳转到定义(, g d
)、跳转到引用列表(, g r
)、向后移动(Ctrl+O
)、向前移动(Ctrl+I
)、添加断点(SPC d b a
)、删除断点(SPC d b d
)、删除全部断点(SPC d b D
)、启动调试(SPC d d d
)。
Python
Spacemacs对Python的支持也比较成熟,首选根据文档进行配置。
dotspacemacs-configuration-layers
'(
(python :variables
python-backend 'lsp
python-lsp-server 'pylsp
python-formatter 'yapf
python-format-on-save t
python-save-before-test nil)
)
国内ELPA源
(defun dotspacemacs/user-init ()
(setq configuration-layer-elpa-archives
'(("melpa-cn" . "http://mirrors.ustc.edu.cn/elpa/melpa/")
("nongnu-cn" . "http://mirrors.ustc.edu.cn/elpa/nongnu/")
("gnu-cn" . "http://mirrors.ustc.edu.cn/elpa/gnu/")))
)
当前行居中
(defun dotspacemacs/user-init ()
(if (not (version< emacs-version "27"))
(add-hook 'window-configuration-change-hook 'recenter nil))
)
VSCode深色主题
(defun dotspacemacs/init ()
(setq-default
dotspacemacs-themes '(vscode-dark-plus
spacemacs-light
spacemacs-dark)
)
)
灵活切换主题及预览,使用SPC T N
然后n或p组合键。
内存剪贴板
Emacs可以利用支持OSC-52协议的Terminal,来操作OS内存剪贴板,目前有个插件clipetty可以做到。注意别在tmux中使用。 配置如下:
dotspacemacs-configuration-layers
'(
clipetty
)
...
(defun dotspacemacs/user-config ()
(global-clipetty-mode)
)
结语
以上便是本次入门Spacemacs的全部内容。2周时间不长,操作习惯还在逐渐稳固。个人感觉全命令行操作能让思绪更聚焦,提高工作效率。