Keyboard shortcut to compact multi-line whitespace into single space
When refactoring code, I find it very useful to quickly delete unnecessary whitespace with <Ctrl+Alt+Space>
:
;; C-M-space should remove multiple whitespace lines into a single space
(defun multi-line-just-one-space (&optional n)
"Multi-line version of just-one-space: Delete all
spaces, tabs and newlines around point,
leaving one space (or N spaces)."
(interactive "*p")
(let ((orig-pos (point)))
(skip-chars-backward " \t\n")
(constrain-to-field nil orig-pos)
(dotimes (i (or n 1))
(if (= (following-char) ?\s)
(forward-char 1)
(insert ?\s)))
(delete-region
(point)
(progn
(skip-chars-forward " \t\n")
(constrain-to-field nil orig-pos t)))))
(global-set-key (kbd "C-M-SPC") 'multi-line-just-one-space)