多環境で Emacs を使う際に、フォントの設定をしていないと等幅フォントが設定されてコードが読み書きしづらいということが起きます。 今回はそういったことを避けるため、Emacs でのフォントの設定方法を紹介します。
検証環境
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=19.10 DISTRIB_CODENAME=eoan DISTRIB_DESCRIPTION="Ubuntu 19.10" $ emacs --version GNU Emacs 26.3 Copyright (C) 2019 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.
変更方法
まずは使用可能フォントを確認します。 Ricty フォントがあるか調べます。
$ fc-list : family | grep -i ricty Ricty Discord Ricty Diminished Discord Ricty Ricty Diminished
Ricty の他に Ricty Diminished もインストールされていることがわかりました。 もし使いたいフォントが無ければインストールしましょう。
- Ricty: プログラミング用フォント Ricty
- Source Code Pro: Source Code Pro
等など。
Emacs で認識されているフォントを確認します。
font-family-list
関数を評価して、Emacs で使えるフォントを表示しましょう。
*scratch* バッファで
(dolist (x (font-family-list)) (print x))
を評価```emacs (dolist (x (font-family-list)) (print x)) ; Enter C-j
"newspaper"
"gothic" ... ```
M-:
(eval-expression)で(member "Ricty" (font-family-list))
を実行してnil
であれば Ricty フォントは使用できない
等の方法があります。 フォントがインストールされており Emacs でも認識できていることが確認できたら、 設定ファイル(init.el)に以下を追記します。
init.el
; Ricty (when (member "Ricty" (font-family-list)) (add-to-list 'default-frame-alist '(font . "Ricty 11")))
これを追記し C-M-x
(eval-defun
)で評価しても即時反映はされません。
即時確認するには *scratch* バッファや M-:
等で (set-default-font "Ricty")
を評価してやるとよいです。
もちろん Emacs を再起動して確認する方法もあります。
フォントの指定方法
GNU Emacs Manual(Fonts - GNU Emacs Manual)によるとフォントの指定の仕方は3通りあるそうです。
Fontconfig pattern
Monospace Monospace-12 Monospace-12:bold DejaVu Sans Mono:bold:italic Monospace-12:weight=bold:slant=italic
GTK font pattern
Monospace 12 Monospace Bold Italic 12
XLFD (X Logical Font Description)
-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1
XLFD は表記が分かりづらいので、Fontconfig か GTK の形式を使うのが良いと思います。
デバッグ
Emacs フォントを設定する際に使用したツール・コマンド等の紹介です。
Emacs
describe-char
またはC-u C-x =
: カーソル位置の文字の情報を表示をします。xft:-PfEd-Ricty-bold-...
の部分を見れば使用されているフォントを確認できます例:
``` position: 2335 of 3645 (64%), column: 22 character: る (displayed as る) (codepoint 12427, #o30213, #x308b) charset: japanese-jisx0208 (JISX0208.1983/1990 Japanese Kanji: ISO-IR-87) code point in charset: 0x246B script: kana syntax: w which means: word category: .:Base, H:2-byte Hiragana, L:Left-to-right (strong), c:Chinese, h:Korean, j:Japanese, |:line breakable to input: type "C-x 8 RET 308b" or "C-x 8 RET HIRAGANA LETTER RU" buffer code: #xE3 #x82 #x8B file code: #xE3 #x82 #x8B (encoded by coding system utf-8-unix) display: by this font (glyph code) xft:-1ASC-Droid Sans Fallback-normal-normal-normal--13----*-0-iso10646-1 (#xBE)
Character code properties: customize what to show name: HIRAGANA LETTER RU general-category: Lo (Letter, Other) decomposition: (12427) ('る')
There are 2 overlays here: From 1 to 3646 after-string [Show] face hiwin-face window #
From 2321 to 2358 face hl-line priority -50 window # There are text properties here: fontified t
[back] ```
describe-font RET <FONT>
: 入力されたフォントの fullname, 格納場所等が表示されます例:
name (opened by): -PfEd-Ricty-normal-normal-normal-*-16-*-*-*-*-0-iso10646-1 full name: Ricty:pixelsize=16:foundry=PfEd:weight=normal:slant=normal:width=normal:scalable=true file name: /home/maya/.fonts/Ricty-Regular.ttf size: 16 height: 18 baseline-offset: 0 relative-compose: 0 default-ascent: 0 ascent: 14 descent: 4 average-width: 8 space-width: 8 max-width: 8
describe-font-sets RET <FONTSET>
: FONTSET 無指定だとデフォルトで現在のフレームで使用されているフォントセットを表示します。例:
Fontset: -1ASC-Liberation Mono-normal-normal-normal-*-13-*-*-*-*-0-fontset-auto1 CHAR RANGE (CODE RANGE) FONT NAME (REQUESTED and [OPENED]) C-@ .. DEL -*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1 .. ʯ (#x80 .. #x2AF) -*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1 [-1ASC-Liberation Mono-normal-normal-normal-*-13-*-*-*-*-0-iso10646-1] ʰ .. ͯ (#x2B0 .. #x36F) -*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1 ...
Shell Command
fc-list
コマンド: インストールされているフォントを表示します
オプション無指定だとフォントのフルパス表示
例:
bash $ fc-list | head -3 /usr/share/fonts/truetype/lato/Lato-Medium.ttf: Lato,Lato Medium:style=Medium,Regular /usr/share/fonts/truetype/noto/NotoSerifHebrew-CondensedLight.ttf: Noto Serif Hebrew,Noto Serif Hebrew Cond Light:style=Condensed Light,Regular /usr/share/fonts/truetype/noto/NotoSans-SemiCondensedExtraLightItalic.ttf: Noto Sans,Noto Sans SemiCondensed ExtraLight:style=SemiCondensed ExtraLight Italic,Italic
:
の後にパターンを指定して絞り込み;fc-list : family
でフォントファミリー表示例:
bash $ fc-list : family | head -3 Noto Sans Canadian Aboriginal,Noto Sans CanAborig Th Noto Serif Sinhala,Noto Serif Sinhala SemiCondensed Black Noto Sans Malayalam,Noto Sans Malayalam SemiCondensed ExtraBold
GUI アプリ
GNOME であれば gnome-font-viewer
でインストールされているフォントを GUI アプリで確認できます。
例: アプリランチャーから Fonts を選択、またはターミナルで gnome-font-viewer を実行