Emacs: Remove Accent Marks

By Xah Lee. Date: . Last updated: .

Here's a emacs command that converts some Unicode characters into ASCII.

For example: “café” ⇒ “cafe”, “naïve” ⇒ “naive”.

aka Remove accent marks, remove diacritics, Zap Gremlins.

(defun xah-asciify-text (&optional φbegin φend)
  "Change European language characters into equivalent ASCII ones, ➢ for example: “café” ⇒ “cafe”.
When called interactively, work on current line or text selection.

URL `http://ergoemacs.org/emacs/emacs_zap_gremlins.html'
Version 2015-06-08"
  (let ((ξcharMap
          ["á\\|à\\|â\\|ä\\|ā\\|ǎ\\|ã\\|å\\|ą" "a"]
          ["é\\|è\\|ê\\|ë\\|ē\\|ě\\|ę" "e"]
          ["í\\|ì\\|î\\|ï\\|ī\\|ǐ" "i"]
          ["ó\\|ò\\|ô\\|ö\\|õ\\|ǒ\\|ø\\|ō" "o"]
          ["ú\\|ù\\|û\\|ü\\|ū"     "u"]
          ["Ý\\|ý\\|ÿ"     "y"]
          ["ç\\|č\\|ć" "c"]
          ["ď\\|ð" "d"]
          ["ľ\\|ĺ\\|ł" "l"]
          ["ñ\\|ň\\|ń" "n"]
          ["þ" "th"]
          ["ß" "ss"]
          ["æ" "ae"]
          ["š\\|ś" "s"]
          ["ť" "t"]
          ["ř\\|ŕ" "r"]
          ["ž\\|ź\\|ż" "z"]
        ξbegin ξend

    (if (null φbegin)
        (if (use-region-p)
            (progn (setq ξbegin (region-beginning)) (setq ξend (region-end)))
          (progn (setq ξbegin (line-beginning-position)) (setq ξend (line-end-position))))
      (progn (setq ξbegin φbegin) (setq ξend φend)))

    (let ((case-fold-search t))
        (narrow-to-region ξbegin ξend)
         (lambda (ξpair)
           (goto-char (point-min))
           (while (search-forward-regexp (elt ξpair 0) (point-max) t)
             (replace-match (elt ξpair 1))))
(defun xah-asciify-string (φstring)
  "Returns a new string. European language chars are changed ot ASCII ones ➢ for example: “café” ⇒ “cafe”.
See `xah-asciify-text'
Version 2015-06-08"
      (insert φstring)
      (xah-asciify-text (point-min) (point-max))

( thanks to robert_nagy for adding chars)

Accumulator vs Parallel Programing

This problem makes a good parallel programing exercise. See: Parallel Programing Exercise: asciify-string.

Alternative Solution with “iconv” or perl

Yuri Khan and Teemu Likonen suggested using the “iconv” shell command. See man iconv. Here's Teemu's code.

(defun asciify-string (string)
"Convert STRING to ASCII string.
For example:
“passé” becomes “passe”"
;; Code originally by Teemu Likonen
    (insert string)
    (call-process-region (point-min) (point-max) "iconv" t t nil "--to-code=ASCII//TRANSLIT")
    (buffer-substring-no-properties (point-min) (point-max))))

Julian Bradfield suggested Perl. Here's his one-liner, it removes chars with accent marks.

perl -e 'use encoding utf8; use Unicode::Normalize; while ( <> ) { $_ = NFKD($_); s/\pM//g; print; }'

Source groups.google.com

Though, it would be nice to have a pure elisp solution, because “iconv” is not in Windows or Mac OS X as of .

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.