Can emacs re-indent a big blob of HTML for me?

后端 未结 10 1988
生来不讨喜
生来不讨喜 2021-01-29 20:09

When editing HTML in emacs, is there a way to automatically pretty-format a blob of markup, changing something like this:

  
10条回答
  •  时光取名叫无心
    2021-01-29 20:51

    i wrote a function myself to do this for xml, which works well in nxml-mode. should work pretty well for html as well:

    (defun jta-reformat-xml ()
      "Reformats xml to make it readable (respects current selection)."
      (interactive)
      (save-excursion
        (let ((beg (point-min))
              (end (point-max)))
          (if (and mark-active transient-mark-mode)
              (progn
                (setq beg (min (point) (mark)))
                (setq end (max (point) (mark))))
            (widen))
          (setq end (copy-marker end t))
          (goto-char beg)
          (while (re-search-forward ">\\s-*<" end t)
            (replace-match ">\n<" t t))
          (goto-char beg)
          (indent-region beg end nil))))
    

提交回复
热议问题