Spacemacs中文字体终极解决方案
安装Spacemacs之后,用Sogou输入法可以直接输入,以为Spacemacs编辑器已经把中文问题完美解决了。使用的过程当中,发现Spacemacs默认配置对中文支持也不是非常完美的。国内使用Emacs的群体不是特别大,Baidu基本上找不到可靠的解决方案,没有google恐怕这个坑是跳不过去了,参考文章当中的各种方案都试了一遍才找到解决方案,最终通过修改中英文默认字体解决了。
修改spacemacs的默认字体
我用的系统是Arch Linux,系统默认的字体是Ubuntu Mono,但Spacemacs默认的字体是Source Code Pro,我打印系统的字体列表,发现并没有这种字体,所以我修改.spacemacs
配置文件,把默认字体改为系统默认字体Ubuntu Mono。1
2
3
4
5
6
7
8
9
10dotspacemacs-default-font '("Source Code Pro"
:size 14
:weight normal
:width normal
:powerline-scale 1.1)
dotspacemacs-default-font '("Ubuntu Mono"
:size 16
:weight normal
:width normal
:powerline-scale 1.1)
打印系统的字体列表的方法
新建一个名为test.el的文件内容如下,用于打印系统的拥有的字体。Spc + m + e + c执行这行代码,在message buffer当中就可以看到系统的字体列表。1
(print (font-family-list))
修改spacemacs的默认中文字体
如果系统中还没有“文泉驿等宽微米黑”字体,可以通过一下代码安装。1
sudo apt-get install ttf-wqy-microhei
为了org-mode下的表格完美对齐,需要增加中文等宽字体配置。修改.spacemacs配置文件,找到defun dotspacemacs/user-config (),添加如下代码设置中文等宽字体。1
2
3
4
5
6
7
8
9
10;;设置中文字体方法一
(dolist (charset '(kana han symbol cjk-misc bopomofo))
(set-fontset-font (frame-parameter nil 'font)
charset (font-spec :family "文泉驿等宽微米黑"
:size 16)))
;;设置中文字体方法二
(set-fontset-font "fontset-default"
(cons (decode-char 'ucs #x4e00)
(decode-char 'ucs #x9fff))
"-*-文泉驿等宽微米黑-*-*-*-*-16-*-*-*-*-*-*-*")
spacemacs官方的layer当中有一个为了改善中文环境下的体验的层chinese layer,我的配置是启用了这个层的,这个layer当中有一个叫做pangu-spacing的包,是用来更好地显示中英文混排的,默认情况下,中英文混排会在英文前后添加1个空格分割显示,但这个空格会影响org-table的显示效果,保存的也时候也会写入到文件当中。我用org-mode来写博客的时候,经常会遇到中英文路径的时候,这个空格就变得很碍事了,需要在org-mode下禁用写入插入的功能。在.spacemacs配置文件中add hook无效,就直接将变量定义在配置文件中。因为pangu-spacing是全局有效的,各种模式下中英文混排都会添加空格,这样的排版方式确实十分美观,但有时多余的空格难免会造成困扰,这个时候只需要M-x pangu-spacing就可以关闭盘古之白。1
2
3
4
5
6
7;;add hook的方法无效
(add-hook 'org-mode (lambda()
(global-pangu-spacing-mode 0)
(set (make-local-variable 'pangu-spacing-real-insert-separtor) nil)))
;;直接定义在(defun dotspacemacs/user-config ()
(global-pangu-spacing-mode 0)
(set (make-local-variable 'pangu-spacing-real-insert-separtor) nil)