在Spacemacs中配置Fira Code并启用ligatures特性

Fira是Mozilla主推的字体系列。Fira Code是其中的一员,专为写程序而生。出来具有等宽等基本属性外,还加入了编程连字特性(ligatures)。编程连字特性,其实就是利用这个特性对编程中的常用符号进行优化,比如把输入的「!=」直接显示成「≠」或者把「>=」变成「≥ 」等等,以此来提高代码的可读性。

作为传说中的程序员,Fira Code也早已称为我的各大常用编辑器标配字体。Fira Code在其他编辑器和终端上配置较为简单,选中字体过后,启用Ligatures Feature即可。Spacemacs和Emacs的配置稍微复杂一些,这里分享一下Fira Code在Spacemacs下的配置。

第一步

第二步

获取并安装 Fira Code Symbol Font。对于Linux系统,可以直接把字体解压并放到 /usr/share/fonts 目录,并重载字体的cache,使其生效。

第三步

在Spacemacs中,使用<SPC> f e d 编辑 ~/.spacemacs配置文件。对于Emacs用户来说,可以编辑 ~/.emacs.d/init.el

在配置文件中,加入如下设置:

1
2
3
4
5
6
7
8
9
10
dotspacemacs-default-font '(("FuraCode Nerd Font Mono"
:size 16
:weight medium
:width normal
:powerline-scale 1.1)
("Fira Code Symbol"
:size 16
:weight normal
:width normal
:powerline-scale 1.1))

第四步

同样是编辑配置文件,在Spacemacs的配置项dotspacemacs-user-config中加入如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(defun dotspacemacs/user-config()
"Configuration function for user code."
;; Font Ligatures
(defun my-correct-symbol-bounds (pretty-alist)
"Prepend a TAB character to each symbol in this alist,
this way compose-region called by prettify-symbols-mode
will use the correct width of the symbols
instead of the width measured by char-width."
(mapcar (lambda (el)
(setcdr el (string ?\t (cdr el)))
el)
pretty-alist))
(defun my-ligature-list (ligatures codepoint-start)
"Create an alist of strings to replace with
codepoints starting from codepoint-start."
(let ((codepoints (-iterate '1+ codepoint-start (length ligatures))))
(-zip-pair ligatures codepoints)))
(setq my-fira-code-ligatures
(let* ((ligs '("www" "**" "***" "**/" "*>" "*/" "\\\\" "\\\\\\"
"{-" "[]" "::" ":::" ":=" "!!" "!=" "!==" "-}"
"--" "---" "-->" "->" "->>" "-<" "-<<" "-~"
"#{" "#[" "##" "###" "####" "#(" "#?" "#_" "#_("
".-" ".=" ".." "..<" "..." "?=" "??" ";;" "/*"
"/**" "/=" "/==" "/>" "//" "///" "&&" "||" "||="
"|=" "|>" "^=" "$>" "++" "+++" "+>" "=:=" "=="
"===" "==>" "=>" "=>>" "<=" "=<<" "=/=" ">-" ">="
">=>" ">>" ">>-" ">>=" ">>>" "<*" "<*>" "<|" "<|>"
"<$" "<$>" "<!--" "<-" "<--" "<->" "<+" "<+>" "<="
"<==" "<=>" "<=<" "<>" "<<" "<<-" "<<=" "<<<" "<~"
"<~~" "</" "</>" "~@" "~-" "~=" "~>" "~~" "~~>" "%%"
"x" ":" "+" "+" "*")))
(my-correct-symbol-bounds (my-ligature-list ligs #Xe100))))
(defun my-set-fira-code-ligatures ()
"Add fira code ligatures for use with prettify-symbols-mode."
(setq prettify-symbols-alist
(append my-fira-code-ligatures prettify-symbols-alist))
(prettify-symbols-mode))
(add-hook 'prog-mode-hook 'my-set-fira-code-ligatures))

第五步

最后,重载Spacemacs/Emacs的配置文件,使配置生效,即可启用Fira Code字体的Ligatures连字特性。

支持原创技术分享,据说打赏我的人,都找到了女朋友!