Emacs: Change Brackets/Quotes ๐Ÿš€

By Xah Lee. Date: . Last updated: .

Here's command to change brackets in text.

For example, here's what you have:

matrix =
   [3, 99, 'a'],
   [2, 77, 'a']

You want:

matrix =
   {3, 99, 'a'},
   {2, 77, 'a'}

Bracket change is useful when converting data from different languages or formats.

More examples:

put this in your Emacs Init File:

(defun xah-change-bracket-pairs (FromChars ToChars)
  "Change bracket pairs to another type or none.
For example, change all parenthesis () to square brackets [].
Works on current block or selection.

In lisp code, FromChars is a string with at least 2 spaces.
paren ( )
french angle โ€น โ€บ
double bracket [[ ]]
It is split by space, and last 2 items are taken as left and right brackets.

ToChars is similar, with a special value of
followed by 2 spaces.
,it means replace by empty string.

URL `http://xahlee.info/emacs/emacs/elisp_change_brackets.html'
Version: 2020-11-01 2023-03-31 2023-08-25 2023-09-29"
   (let ((xbrackets
            "square [ ]"
            "brace { }"
            "paren ( )"
            "greater < >"
            "double quote \" \""
            "single quote ' '"
            "emacs ` '"
            "markdown grave accent ` `"
            "double square [[ ]]"
            "tilde ~ ~"
            "equal = ="
            "curly double quote โ€œ โ€"
            "curly single quote โ€˜ โ€™"
            "french angle โ€น โ€บ"
            "french double angle ยซ ยป"
            "corner ใ€Œ ใ€"
            "white corner ใ€Ž ใ€"
            "lenticular ใ€ ใ€‘"
            "white lenticular ใ€– ใ€—"
            "angle ใ€ˆ ใ€‰"
            "double angle ใ€Š ใ€‹"
            "tortoise ใ€” ใ€•"
            "white tortoise ใ€˜ ใ€™"
            "white square ใ€š ใ€›"
            "white paren โฆ… โฆ†"
            "white curly bracket โฆƒ โฆ„"
            "pointing angle โŒฉ โŒช"
            "angle with dot โฆ‘ โฆ’"
            "curved angle โงผ โงฝ"
            "math square โŸฆ โŸง"
            "math angle โŸจ โŸฉ"
            "math double angle โŸช โŸซ"
            "math flattened parenthesis โŸฎ โŸฏ"
            "math white tortoise shell โŸฌ โŸญ"
            "heavy single quotation mark ornament โ› โœ"
            "heavy double turned comma quotation mark ornament โ โž"
            "medium parenthesis ornament โจ โฉ"
            "medium flattened parenthesis ornament โช โซ"
            "medium curly ornament โด โต"
            "medium pointing angle ornament โฌ โญ"
            "heavy pointing angle quotation mark ornament โฎ โฏ"
            "heavy pointing angle ornament โฐ โฑ"
            "none  "
     (let ((completion-ignore-case t))
        (completing-read "Replace this:" xbrackets nil t nil nil (car xbrackets))
        (completing-read "To:" xbrackets nil t nil nil (car (last xbrackets)))))))
  (let (xp1 xp2 xleft xright xtoL xtoR)
    (let ((xbds (xah-get-bounds-of-block-or-region))) (setq xp1 (car xbds) xp2 (cdr xbds)))
    (let ((xsFrom (last (split-string FromChars " ") 2))
          (xsTo (last (split-string ToChars " ") 2)))

      ;; (when (< (length xsFrom) 3)
      ;; (error "cannot find input brackets %s" xsFrom))

      ;; (when (< (length xsTo) 3)
      ;;   (message "replace blacket is empty string")
      ;;   (setq xsTo (list "" "" "")))

      (setq xleft (car xsFrom)  xright (car (cdr xsFrom))
            xtoL (car xsTo) xtoR (car (cdr xsTo)))

          (narrow-to-region xp1 xp2)
          (let ((case-fold-search nil))
            (if (string-equal xleft xright)
                (let ((xx (regexp-quote xleft)))
                  (goto-char (point-min))
                       (format "%s\\([^%s]+?\\)%s" xx xx xx)
                       nil t)
                    (overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
                    (replace-match (concat xtoL "\\1" xtoR) t)))
                  (goto-char (point-min))
                  (while (search-forward xleft nil t)
                    (overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
                    (replace-match xtoL t t)))
                  (goto-char (point-min))
                  (while (search-forward xright nil t)
                    (overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
                    (replace-match xtoR t t)))))))))))

requires package Emacs: xah-get-thing.el

Emacs, Work with Brackets