When working on a piece of code it very common that you reformat it several times. You split lines that are too long into multiple lines. Refactoring of some parts makes the code shorter, and so on. Whenever you combine two lines into one you usually get a bunch of spaces between the end of the original line and the beginning of the line you added to it. How much depends on the amount of indentation of the added line. To quickly and easily clean up that mess there a nifty function in Emacs. It’s called cycle-spacing and it will remove all spaces except one between two characters. Try it out by running
While I mostly use the command to contract blocks of spaces into a single one cycle-spacing can do more than that. The first time you run the command it contracts all spaces into a single one. The second time it removes all spaces, and the third time it restores the original spacing. By binding it to a convenient keyboard shortcut (I use
C-S-SPC) you can quickly wrangle the text into shape.
To bind the command to a shortcut of your choice, put the following in your
(global-set-key (kbd "<key sequence, in my case: C-S-SPC>") 'cycle-spacing).
For more nifty tricks in Emacs check out Emacs nifty tricks.