Emacs 黒背景に青文字が見にくいときの対応方法

概要

EmacsのシェルからRestAPIを叩いてjqで整形したときに、keyが黒背景に青文字となって見にくくて困っていました。

下記ではその色を再現できていませんが、”login”,”id”,”node_id”… が濃い青文字となっていました。

$ curl -s https://api.github.com/repos/octocat/Hello-World | jq '.owner'
{
  "login": "octocat",
  "id": 583231,
  "node_id": "MDQ6VXNlcjU4MzIzMQ==",
  "avatar_url": "https://avatars.githubusercontent.com/u/583231?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/octocat",
  "html_url": "https://github.com/octocat",
  "followers_url": "https://api.github.com/users/octocat/followers",
  "following_url": "https://api.github.com/users/octocat/following{/other_user}",
  "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
  "organizations_url": "https://api.github.com/users/octocat/orgs",
  "repos_url": "https://api.github.com/users/octocat/repos",
  "events_url": "https://api.github.com/users/octocat/events{/privacy}",
  "received_events_url": "https://api.github.com/users/octocat/received_events",
  "type": "User",
  "site_admin": false
}

対応方法

Bus errorとSegmentation faultに困ったら見るブログ 【emacs】黒字に青が見にくいときに、色合いを変更する方法
黒い背景のterminalでemacsを起動すると青色の文字がとても見にくい・・・そういうときは設定で色を変更する.emacs にそれぞれ書き加えても良いが、色合いを見ながら変更する方法があった

上記を参考に、list-faces-display で対象の色を探して変更しました。

M-x list-faces-display

結果的には下記の設定が反映されていました。

既定の設定

(custom-set-faces
 '(ansi-color-blue ((t (:background "blue2" :foreground "blue2"))))
)

変更後の設定

(custom-set-faces
 '(ansi-color-blue ((t (:background "blue2" :foreground "deep sky blue"))))
)

参考情報

curlの表示そのままだと色付けされず、jqを通すと色付けされるのは、実はjqの出力にはANSIの制御コードが含まれているからということがわかりました。

下記の設定を行い、色付けをOFFにしてみます。

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-off)

そうすると出力結果は下記のようになります。

$ curl -s https://api.github.com/repos/octocat/Hello-World | jq '.owner'
^[[1;39m{
  ^[[0m^[34;1m"login"^[[0m^[[1;39m: ^[[0m^[[0;32m"octocat"^[[0m^[[1;39m,
  ^[[0m^[[34;1m"id"^[[0m^[[1;39m: ^[[0m^[[0;39m583231^[[0m^[[1;39m,
.
.
.

下記のようにファイルに保存すると制御コードは除去されます。 (私は、これまで全く意識していませんでした。)

curl -s https://api.github.com/repos/octocat/Hello-World | jq '.owner' > Hello-World.owner.json

ansi-color-blue については、下記で定義(defface)されています。

ansi-color.el (/usr/local/share/emacs/29.1/lisp/ansi-color.el.gz)

(defface ansi-color-blue
  '((t :foreground "blue2" :background "blue2"))
  "Face used to render blue color code."
  :group 'ansi-colors
  :version "28.1")

ansi-color の色種類は限られており、その中の”青”として定義されていたのが blue2 であったということがわかりました。

(defcustom ansi-color-names-vector
  ["black" "red3" "green3" "yellow3" "blue2" "magenta3" "cyan3" "gray90"]
.
.
.
  :group 'ansi-colors)
Copied title and URL