💾 Archived View for idiomdrottning.org › fix-spaces captured on 2024-08-25 at 00:53:06. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-02-05)

-=-=-=-=-=-=-

Fixing mistyped spaces

An Emacs function that reads a single char, then runs a query replace backwards from where you are, letting you optionally replace that char with spaces. After the query replace it then sends you back to where you were.

It’s a vanilla query replace which has lots of convenient ways to get out of it or tweak the text at point and so on. One that I learned today is period for “yes, but this is the last one”, which can be pretty convenient.

You can also use E to change the replacement string with something else than a space.

(defun fix-spaces ()
  (interactive)
  (query-replace (char-to-string (read-char)) " " nil nil nil t)
  (goto-char (mark)))

Probably it’s only me doing this but when I type on the tablet’s touchscreen keyboard, my hands are so tiny that I often miss the space bar and instead hit b, n, or m or even v sometimes.

Trying it out for a bit I’m wondering if maybe “fix-anything” is a better version, which reads two characters instead of assuming the second is a space.

(defun fix-anything ()
  (interactive)
  (query-replace (char-to-string (read-char)) (char-to-string (read-char)) nil nil nil t)
  (goto-char (mark)))

Here is another fun variant that just replaces one match of any character with any other:

(defun replace-one-character-backwards ()
  (interactive)
  (save-excursion
    (when (re-search-backward (char-to-string (read-char)) nil t)
     (replace-match (char-to-string (read-char))))))