Wednesday, September 16, 2026

A Niftier Way To ASCIIfy An Emacs Buffer

Web page content lately is littered with unusual Unicode characters that resemble ASCII characters. It's possible this is how an author (whether human or AI) might "sign" work.

I use a recorded keystroke macro to brute-force-edly replace such Unicode characters with their ASCII equivalents. This allows me to maintain ASCII-compatible files. For example, to replace the right single quote character having a character code of #x2019, the macro replays M-% ’ RET ' RET ! to replace all occurrences with the straight apostrophe, #x27.

I'm not trying to publish someone else's work as my own -- I do provide a reference to source whenever possible. I'm simply trying to keep my content as unadulterated as possible.

There are at least three problems with my macro:

  • If #x2019 can't be found, an exclamation mark is placed at point.
  • It fails to replace other single-quote-like Unicode characters such as #x2018 or #x2032.
  • The method is clunky and inelegant to an obscene degree.

However, I came across a function in replace.el that led me to find replace-char-fold, a very interesting custom variable.1

According to its docstring, "Non-nil means replacement commands should do character folding in matches. This means, for instance, that \\=' will match a large variety of Unicode quotes." This is exactly what I need!

Now I have Yet Another Fun Emacs Project (YAFEP), which is to create a sleek and robust ASCIIfy Lisp function. And YAFEP aligns fortuitously with this month's Emacs Carnival topic, which is Games.

What's been your experience with quoting web content?


1 For a deeper dive into character folding in Emacs, please read the info node on lax matching.

These pages on Wikipedia served as references for this article:

No comments:

Post a Comment

Thank you for taking time to comment. Please note that comments may be deleted if they:

* Contain link(s) to a domain outside USA or Canada or to a commercial enterprise.
* Include non-English words or characters.
* Are irrelevant to the subject matter of the post.

For posts older than 14 days, comments are moderated and require approval.