0

I would like to be able to go from this:

<label for="exampleInputEmail1" class="form-label">Phone number</label>
<input type="text" class="form-control" id="phone" aria-describedby="phoneHelp">
<div id="phoneHelp" class="form-text">This is in case of quick questions.</div>

to this (very long line intentional):

<label for="exampleInputEmail1" class="form-label">Phone number</label><input type="text" class="form-control" id="phone" aria-describedby="phoneHelp"><div id="phoneHelp" class="form-text">This is in case of quick questions.</div>

In other words, delete indenting and collapsed whitespace between tags in a selection in an HTML buffer. The general goal is making the file more legible, as the collapsed portions typically are repeated form elements that differ only for a small detail.

0

Does this do what you want?

(defun collapse ()
    (interactive)
    (replace-regexp "> *[\n]+ *<" "><"))

Edited:

Just on selection:

(defun collapse ()
  (interactive (if (use-region-p)
                   (progn
                     (setq start (region-beginning))
                     (setq end (region-end))
                     (goto-char start)
                     (while
                         (re-search-forward "> *[\n]+ *<" end t)
                       (replace-match "><" nil nil))))))
1
  • Hmm... almost. It looks like it does that in the whole buffer, not just on the selection. – simone Feb 12 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.