Linebreak “Fixing” in Emacs

Screenshot - 02082013 - 11:15:28 AMBack in the stone ages, everybody knew that whitespace was unimportant and people should be free to format text, code, whatever, any damn way they please.  NO LONGER!  In our brave new world all those line breaks, indents, tabbing, and so on are all critical and we must all do them the same way!  Or else!

The latest offender is GitLab‘s slightly modified Markdown, which treats linebreaks in text blocks as actual linebreaks and inserts hard breaks in the output HTML. Somewhat disappointing for an otherwise largely stellar package…

In any event, that means you need to be careful about how you format your and such if you want it to print reasonably in GitLab’s web view. For all those Emacs devotees used to religiously META-Q‘ing text paragraphs to be terminal formatted—as any sane person would!—you wind up with a ton of short lines in the display.

This is a little helper I wrote and include in my .emacs to deal with this and similar problems:

(defun fix-lines ()
  (interactive "*")
      (if (region-active-p)
          (progn  (setq start (region-beginning) end (region-end))
                  (narrow-to-region start end)
                  (goto-char (point-min))))
         (while (re-search-forward "\\([^\n]\\)\n\\([^\n]\\)" nil t)
              (replace-match "\\1 \\2" nil nil)))))

(global-set-key "\C-xf" 'fix-lines)

CTRL-xf will now get rid of all those newly eviiil linebreaks. If a region is active only that text will be affected, otherwise the text from the current point on is modified. Hooray!