LoginSignup
13

More than 5 years have passed since last update.

Emacsで #f95327 などの文字列に色を付けておく

Last updated at Posted at 2013-09-15

css を書いてると #f92732 とか出てくるけど、どんな色なのかパッとわからないと不便だと思ったのでEmacsで色を付けてみた。

コメント欄で yasuyk@github さんが教えてくれた rainbow-mode の方が便利そうなので書き変え。

M-x package-install で rainbow-mode をインストールして

~/.emacs.d/inits/30-rainbow.el
(require 'rainbow-mode)
(add-hook 'css-mode-hook 'rainbow-mode)
(add-hook 'scss-mode-hook 'rainbow-mode)
(add-hook 'php-mode-hook 'rainbow-mode)
(add-hook 'html-mode-hook 'rainbow-mode)

でhookを書けておけばOK。ちなみにこの書き方は24からなので23より下のバージョンを使ってる人は「参考」を見て書いてください。

--こっから下は古いの。

~/.emacs.d/inits/40-hex-color.el
(defvar hexcolour-keywords
  '(("#[abcdef[:digit:]]\\{6\\}"
     (0 (put-text-property
         (match-beginning 0)
         (match-end 0)
         'face (list :background
                     (match-string-no-properties 0)))))))
(defun hexcolour-add-to-font-lock ()
  (font-lock-add-keywords nil hexcolour-keywords))

(add-hook 'css-mode-hook 'hexcolour-add-to-font-lock)
(add-hook 'scss-mode-hook 'hexcolour-add-to-font-lock)
(add-hook 'php-mode-hook 'hexcolour-add-to-font-lock)
(add-hook 'html-mode-hook 'hexcolour-add-to-font-lock)

でOK。

参考

Popup color from color string like #f2d3aa with Emacs - Stack Overflow
http://stackoverflow.com/questions/18806523/popup-color-from-color-string-like-f2d3aa-with-emacs

How to enable a package/mode on emacs startup? - Stack Overflow
http://stackoverflow.com/questions/16048231/how-to-enable-a-package-mode-on-emacs-startup

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13