Deleting Extraneous Spaces In Emacs

Charles Choi has a nice post that reminded me about something I know but never take advantage of: the Emacs cycle-spacing command. As Choi points out, we often find there are additional spaces in our text that we want to delete. That’s not hard. You just navigate to where the extra spaces are and use Back Space or Ctrl+d to delete the unneeded spaces.

Still, that seems like a lot of bother to us Emacsers who are always on the lookout for ways to reduce our work and keystrokes. An easy way to reduce keystrokes for deleting the extra spaces is the cycle-spacing command that will, on consecutive invocations,

  • Replace all spaces with a single space
  • Remove all spaces
  • Restore the original spacing

Even given a simple keybinding for cycle-spacing that’s almost always too much work because—at least in my experience—there is usually only a single space to be deleted. For me, the real value of Choi’s post is his offhand remark that Avy makes it easy to navigate to the excess spaces.

Choi doesn’t elaborate but I assume he means something along the lines of using avy-goto-char-timer with two spaces as the argument. Since I have avy-goto-char-timer bound to Hyper+s, it’s really fast to get the target area. Doubtless, those of you using Helm or some other navigation aid have a similar command to get you to the right place.

If you often find yourself with more than one extra spaces, cycle-spacing may be worth internalizing. Even if you only have a single extra space, it may be worth writing a little Elisp to search for the extra spaces and delete them with cycle-spacing.

This entry was posted in General and tagged . Bookmark the permalink.