LoginSignup
73
70

More than 5 years have passed since last update.

whitespace-modeを使って、ファイルの保存時に行末のスペースや末尾の改行を削除する

Posted at

whitespace-modeは、スペースやタブなどの不可視文字を見えるようにするモードですが、それだけでなく不要なスペースや改行を削除するための機能もあります。
これを使って、ファイルの保存時に行末のスペースや末尾の改行を削除できるようにします。

不要なスペース・改行の削除

whitespace-modeの関数whitespace-cleanupを呼び出すと不要なスペースなどを削除できます。
だたし、whitespace-styleに設定した値によって、何を削除対象とするかの挙動が変わってしまうので注意が必要です。

変数(リスト)whitespace-styleに含めることのできるシンボルと、それが意味する削除対象のうち、主なものは以下の通りです。

シンボル 削除対象
empty バッファの先頭と末尾の空行
trailing 行末のスペース

その他にも、行頭の余計なスペースを削除したり、インデントに使うタブとスペースの置換も行えるみたいです。
詳しくは、EmacsWiki: White Space を参照してください。

保存時に実行

whitespace-cleanupbefore-save-hookに登録すれば、ファイルの保存時に実行してくれるようになります。
ただ、実行のオン・オフを切り替えるのが少し面倒なので、もう少し簡単に保存時の実行トリガーを設定することができる、whitespace-modewhitespace-actionを利用します。

whitespace-actionは変数(リスト)であり、特定の値を設定しておくと、whitespace-modeがオンになったときや、ファイルの保存時などに、whitespace-modeに特定の動作をさせることができます。

whitespace-actionに含めることのできるシンボルと、それが意味する動作のうち、主なものは以下のとおりです。

シンボル 動作
cleanup whitespace-modeがオンになった時に、削除実行
auto-cleanup ファイルの保存時に、削除実行

設定

以下のようになりました。
faceのwhitespace-emptyを大人しめの色などに設定しておかないと、デフォルトだと黄色に表示されるので、かなり見づらいです。

(require 'whitespace)
(setq whitespace-style '(face           ; faceで可視化
                         trailing       ; 行末
                         tabs           ; タブ
                         spaces         ; スペース
                         empty          ; 先頭/末尾の空行
                         space-mark     ; 表示のマッピング
                         tab-mark
                         ))

(setq whitespace-display-mappings
      '((space-mark ?\u3000 [?\u25a1])
        ;; WARNING: the mapping below has a problem.
        ;; When a TAB occupies exactly one column, it will display the
        ;; character ?\xBB at that column followed by a TAB which goes to
        ;; the next TAB column.
        ;; If this is a problem for you, please, comment the line below.
        (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t])))

;; スペースは全角のみを可視化
(setq whitespace-space-regexp "\\(\u3000+\\)")

;; 保存前に自動でクリーンアップ
(setq whitespace-action '(auto-cleanup))

(global-whitespace-mode 1)

(defvar my/bg-color "#232323")
(set-face-attribute 'whitespace-trailing nil
                    :background my/bg-color
                    :foreground "DeepPink"
                    :underline t)
(set-face-attribute 'whitespace-tab nil
                    :background my/bg-color
                    :foreground "LightSkyBlue"
                    :underline t)
(set-face-attribute 'whitespace-space nil
                    :background my/bg-color
                    :foreground "GreenYellow"
                    :weight 'bold)
(set-face-attribute 'whitespace-empty nil
                    :background my/bg-color)

こんな風に表示されます。
whitespace.png

markdown-modeなどでauto-cleanupを無効にする

フックを使って、whitespace-actionnilに設定してやればいいです。
whitespace-actionはグローバル変数なので、make-local-variableでバッファローカル変数にしています。

(add-hook 'markdown-mode-hook
          '(lambda ()
             (set (make-local-variable 'whitespace-action) nil)))
73
70
0

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
73
70