delete extra blank lines in emacs

I don't know about a built in function for this, but you can do:

M-x single-lines-only

(defun single-lines-only ()
  "replace multiple blank lines with a single one"
  (interactive)
  (goto-char (point-min))
  (while (re-search-forward "\\(^\\s-*$\\)\n" nil t)
    (replace-match "\n")
    (forward-char 1)))

C-x h M-x replace-regexp RET ^ C-q C-j C-q C-j + RET C-q C-j RET

which marks the whole buffer and replaces two or more blank lines with a single blank line.


C-x C-o (delete-blank-lines) does exactly that. You'll just need a little macro magic to run it on a whole buffer.

Tags:

Emacs

Regex