バックアップファイルの保存場所

メインのテキストエディタ秀丸エディタからEmacsへ移行する。

理由は

  • Emacs は複数プラットフォームでの動作がサポートされている
  • いろいろと便利な使い方ができる
    • デバッグを実行したり
    • ビルドを実行したり
    • その他...

Emacsへの移行を考えていたところにちょうどよい本が発売された。

Emacs実践入門 ?思考を直感的にコード化し、開発を加速する (WEB+DB PRESS plus)

Emacs実践入門 ?思考を直感的にコード化し、開発を加速する (WEB+DB PRESS plus)


ざっと読んだ感じでは、これからEmacsを使おうとする人にとってかなりよさげ。


逆に最初に「入門GNU Emacs」なんかを手に取ってしまうと結構しんどいかな。
私は買ってしまったけど・・・

入門 GNU Emacs 第3版

入門 GNU Emacs 第3版


Emacs の挙動としてまず気になるのが、カレントディレクトリにバックアップファイルが作成されること。
なので、手始めにバックアップファイルとオートセーブファイルを一カ所 (~/.emacs.d/backups/) へ保存することにした。

~/.emacs.d/init.el

;;; --- バックアップとオートセーブ ---
;; バックアップファイルとオートセーブファイルを ~/.emacs.d/backups/ へ集める
(add-to-list 'backup-directory-alist
             (cons "." "~/.emacs.d/backups/"))
(setq auto-save-file-name-transforms
        `((".*" ,(expand-file-name "~/.emacs.d/backups/") t)))

入門書を参考にほぼそのまま設定しているのできちんと理解できていない部分もあるのだが、解釈した内容を以下に書いておく。


backup-directory-alist の説明

Alist of filename patterns and backup directory names.
Each element looks like (REGEXP . DIRECTORY). Backups of files with
names matching REGEXP will be made in DIRECTORY. DIRECTORY may be
relative or absolute. If it is absolute, so that all matching files
are backed up into the same directory, the file names in this
directory will be the full name of the file backed up with all
directory separators changed to `!' to prevent clashes. This will not
work correctly if your filesystem truncates the resulting name.

For the common case of all backups going into one directory, the alist
should contain a single element pairing "." with the appropriate
directory name.

If this variable is nil, or it fails to match a filename, the backup
is made in the original file's directory.

On MS-DOS filesystems without long names this variable is always
ignored.


要約すると

  • 全てのバックアップを一つのディレクトリへ放り込みたい場合は "." とディレクトリのペアを設定すればよい

ということかな。


auto-save-file-name-transforms の説明

Transforms to apply to buffer file name before making auto-save file name.
Each transform is a list (REGEXP REPLACEMENT UNIQUIFY):
REGEXP is a regular expression to match against the file name.
If it matches, `replace-match' is used to replace the
matching part with REPLACEMENT.
If the optional element UNIQUIFY is non-nil, the auto-save file name is
constructed by taking the directory part of the replaced file-name,
concatenated with the buffer file name with all directory separators
changed to `!' to prevent clashes. This will not work
correctly if your filesystem truncates the resulting name.

All the transforms in the list are tried, in the order they are listed.
When one transform applies, its result is final;
no further transforms are tried.

The default value is set up to put the auto-save file into the
temporary directory (see the variable `temporary-file-directory') for
editing a remote file.

On MS-DOS filesystems without long names this variable is always
ignored.

要約するとこんな感じだろうか。

  • ファイル名が REGEXP に一致したファイルが対象
  • 対象ファイルは REPLACEMENT に指定されたディレクトリへ保存される
  • UNIQUIFY が non-nil の場合はファイル名にディレクトリ名を含む。その際ディレクトリの区切り文字は '!' に置き換えられる