Emacs Tip: just-one-space

In writing text or even code in Emacs, lets say you have some thing like this

a friend of mine _        has gone to build a Snow-Man.

The cursor is at _ in the above example. So you need to remove the unnecessary spaces in the sentence.

One way to do this is pressing M-` delete all the spaces in between and `SPC. That is fine what if you have new lines like this

A friend of mine _


        has gone to build a Snow-Man.

Its very painful to bring them to gather.

That is where M-x just-one-space comes in. With a negative argument it takes care of any number of newlines. This is the code I use to handle this

(define-key global-map (kbd "C-|")
  (lambda ()
  "place only one space in between at the point"
  (interactive)
  (just-one-space -1)))

Try this, it will definitely come handy. Of course change C-| to your convenient key chord.