notes from /dev/null

by Charles Choi 최민수


Precise Word Spacing in macOS Emacs

30 Dec 2024  Charles Choi

Getting precise single horizontal spacing between words in most editors is usually a laborious process:

  1. Navigate the cursor to the point where there is excess space.
  2. Use the backspace or delete-forward key to get the desired spacing.

Both steps can involve a fair amount of key-presses and possible mouse-clicks, with navigation requiring the attention needed to move the cursor to the excess space, and the deletion (either backwards or forwards) needing to be repeated for however many extra spaces there are.

If you using Emacs though, you’re in luck: the deletion command cycle-spacing when called multiple times will successively:

  1. Replace all spaces with a single space.
  2. Remove all spaces.
  3. Restore the original spacing.

In the Pragmatic Emacs post on Cycle Spacing, the guidance there is to globally bind cycle-spacing to M-SPC. However this is problematic on macOS, as M-SPC (aka ⌘-SPC) is reserved for opening the Spotlight window.

An alternate keybinding I use instead is M-\. (By default M-\ is set to delete-horizontal-space.)

(keymap-global-set "M-\\" #'cycle-spacing)

Repeating M-\ twice will achieve the same result as delete-horizontal-space so life is good. (As always, key-binding choice is a preference. Use a binding that works for you.)

Also note that the invoking the prefix argument C-u n M-\ will create a horizontal space of length n.

If you have Avy installed, keyboard navigation to the excess space is trivial. It’s a great combo, especially if you like a keyboard-only workflow.

If cycle-spacing is TIL to you, consider yourself informed.

More reading on the deletion commands can be found in Deletion (GNU Emacs Manual).

emacs

 

AboutMastodonBlueskyGitHub

Feeds & TagsGet Captee for macOS

Powered by Pelican