如何自动缩进多个文件?
How to auto indent multiple files?
目前,我生成的是静态文件,但这些文件的缩进不正确。 Emacs 的自动缩进与 c-x h tab
配合使用效果很好,但这是针对每个文件的。我想自动缩进多个文件(大约 50 个,因此手动执行是不可行的)。
有什么办法可以做到吗?无论是使用不同的文本编辑器还是脚本等。如果有帮助,大多数文件都是 .html
.
我用一些 HTML 文件测试了下面的代码,它运行良好。
(defun indent-file (file-name)
(save-window-excursion
(find-file file-name)
(indent-region (point-min) (point-max))
(write-region nil nil file-name)))
;; argv is a list stores command line option. In this case, it will be ("/your/directory/path").
;; directory-files-recursively will find files recursively, and it needs emacs 25.1 or later.
(let* ((target-dir (car argv))
(target-file-names (directory-files-recursively target-dir ".*.html$")))
(dolist (file-name target-file-names)
(indent-file file-name)))
- 将上面的代码保存为'indent-files.el'
- 运行
emacs --script indent-files.el "/your/directory/path"
在终端
如果你想将 emacs lisp 用作通用脚本语言,这将很棘手,尽管它确实可以。这里有一些提示:https://swsnr.de/blog/2014/08/12/emacs-script-pitfalls/
目前,我生成的是静态文件,但这些文件的缩进不正确。 Emacs 的自动缩进与 c-x h tab
配合使用效果很好,但这是针对每个文件的。我想自动缩进多个文件(大约 50 个,因此手动执行是不可行的)。
有什么办法可以做到吗?无论是使用不同的文本编辑器还是脚本等。如果有帮助,大多数文件都是 .html
.
我用一些 HTML 文件测试了下面的代码,它运行良好。
(defun indent-file (file-name)
(save-window-excursion
(find-file file-name)
(indent-region (point-min) (point-max))
(write-region nil nil file-name)))
;; argv is a list stores command line option. In this case, it will be ("/your/directory/path").
;; directory-files-recursively will find files recursively, and it needs emacs 25.1 or later.
(let* ((target-dir (car argv))
(target-file-names (directory-files-recursively target-dir ".*.html$")))
(dolist (file-name target-file-names)
(indent-file file-name)))
- 将上面的代码保存为'indent-files.el'
- 运行
emacs --script indent-files.el "/your/directory/path"
在终端
如果你想将 emacs lisp 用作通用脚本语言,这将很棘手,尽管它确实可以。这里有一些提示:https://swsnr.de/blog/2014/08/12/emacs-script-pitfalls/