Skip to main content
1 of 3

Thats what I'm using, not specific go any mode. Honors region if existing:

(defun just-one-empty-line (&optional beg end)
  "Delete consecutive empty lines, retain just one.

Delete trailing whitespace "
  (interactive "*")
  (let ((beg (cond (beg)
           ((region-active-p)
            (region-beginning))
           (t (point-min))))
    (end (cond (end (copy-marker end))
           ((region-active-p)
            (copy-marker (region-end)))
           (t (copy-marker (point-max))))))
    (save-excursion
      (save-restriction
        (narrow-to-region beg end)
        (goto-char beg)
        (while (not (eobp))
          (while (looking-at "^\\([ \t]*\n\\)[ \t]*$")
            (delete-region (match-beginning 1) (match-end 1)))
          (forward-line 1)))
      (widen))))