要清理一些文件,我需要执行一系列步骤,这些步骤的 emacs 代码是
(query-replace "," " " nil
(if (and transient-mark-mode mark-active)
(region-beginning))
(if (and transient-mark-mode mark-active)
(region-end)))
(query-replace "1 1/4" "1.25" nil
(if (and transient-mark-mode mark-active)
(region-beginning))
(if (and transient-mark-mode mark-active)
(region-end)))
(query-replace-regexp "[0-9][0-9][0-9]V[0-9][0-9][0-9] " "" nil
(if (and transient-mark-mode mark-active)
(region-beginning))
(if (and transient-mark-mode mark-active)
(region-end)))
(还有更多,但你明白了)有没有办法将所有这些命令放在一个文件中并运行它们?或者也许在 lisp 文件中为它们分配一个名称并按名称运行它们?
您可以参考手册的这一部分。把你想做的所有事情都放在一个大型功能foo
中。把它放到~/bar.el
.然后这将完成工作:
emacs --batch -l ~/bar.el -f foo
UPD:一个小例子
投入~/bar.el
:
(defun foo ()
(goto-char (point-min))
(replace-regexp "foo" "bar")
(goto-char (point-min))
(replace-regexp "fred" "barney")
(save-buffer))
创建测试文件:
echo "Fred walks into a foo" > test.txt
现在测试它:
emacs --batch -l ~/bar.el test.txt -f foo