emacs-用gpg加密org文件
1. 安装GPG
Linux/MacOS不用多谈,Windows上最简单的就是用 scoop install gpg
安装 gpg
,然后重启电脑,确保Emacs的 shell
重载环境变量。
2. 使用GPG加密
参见这篇Worg指南
使用密钥的好处是可以利用gpg的缓存功能,减少保存时的密码输入。尤其在使用多个GPG文件时,优势尽显。
导入密钥:
gpg --full-gen-key #生成时会要求输入name #导出私钥 gpg --armor --output name.key --export-secret-keys #导入私钥 gpg --import name.key #信任私钥 gpg --edit-key name # 看到提示符后输入 trust # 再选择信任等级,我自己的 Key,我选 5
注意:不要使用类似 gpg --armor --export-key XYZ >pubkey_xyz.asc
这样的 Powershell 重定向,将导致生成一个 gpg 无法导入的文本文件(来源)。
3. 其他设置
如果使用了 org-crypt
,需要关闭自动的backup文件:
(setq make-backup-files nil)
同时,如果在Windows上使用,还需注意 utf-8-dos
导致的换行符问题,需要进行一下包装:
(defun my/org-encrypt-entries () (interactive) (org-encrypt-entries) "移除行尾 ^M(CRLF) Replace DOS eolns CR LF with Unix eolns CR" (save-excursion (goto-char (point-min)) (while (search-forward "\r" nil t) (replace-match ""))))
如果使用了懒猫的 auto-save
,需要设置取消自动保存GPG文件:
;;; custom predicates if you don't want auto save. ;;; disable auto save mode when current filetype is an gpg file. (setq auto-save-disable-predicates '((lambda () (string-suffix-p "gpg" (file-name-extension (buffer-name)) t))))