Emacs: xah-replace-pairs.el

By Xah Lee. Date: . Last updated: .

xah-replace-pairs.el is a emacs lisp package for doing multi-pair find replace. (the package was named “xfrp_find_replace_pairs.el”)

Here's a sample use:

(require 'xah-replace-pairs)

(xah-replace-pairs-in-string
 "abcdef"
 [["a" "1"]
  ["b" "2"]
  ["c" "3"]])
;; returns "123def"

Download At

Exported Functions

The package has these functions:

For each function, there's a plain text version and a regex version. They are separate functions so it's simpler for user.

Each function also has a string and region version. The string version works on a given string, the region version works on a region in buffer.

Usage Examples

(require 'xah-replace-pairs)

(defun xah-replace-latex-to-unicode (&optional Begin End)
  "Replace TeX markup to Unicode in current line or selection.
Example: \\alpha becomes α.
Version: 2015-04-28 2023-11-25"
  (interactive)
  (let (xp1 xp2)
    (if (and Begin End)
        (setq xp1 Begin xp2 End)
      (if (region-active-p)
          (setq xp1 (region-beginning) xp2 (region-end))
        (setq xp1 (line-beginning-position) xp2 (line-end-position))))
    (xah-replace-pairs-region
     xp1 xp2
     '(
       ["\\rightarrow" "→"]
       ["\\Sigma" "Σ"]
       ["\\times" "×"]
       ["\\alpha" "α"]
       ["\\beta" "β"]
       ["\\gamma" "γ"]
       ["\\delta" "δ"]
       ["\\Lambda" "Λ"]
       ["\\epsilon" "ε"]
       ["\\omega" "ω"]
       ["\\cup" "∪"]
       ["\\in" "∈"]
       ) t t)))

Find Replace Feedback Loop Problem

xah-replace-pairs-in-string and xah-replace-pairs-region do not have Find Replace Feedback Loop Problem.

It guarantees that a replacement is done IF AND ONLY IF the original input string contains a substring in one of your find string.