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:


Here's the code.

Can also delete the brackets.

(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.
e.g.  \"( paren )\", \"[[ double bracket ]]\" etc.
where the chars before first space is the left bracket, and char after the last space is the right bracket.
(the middle is for convenience for user to type the char name in prompt.)
ToChars is similar, with a special value of \" none \", replace by empty string.

URL `http://xahlee.info/emacs/emacs/elisp_change_brackets.html'
Version: 2020-11-01 2022-04-07 2022-07-05 2023-03-31"
  (interactive
   (let ((xbrackets
          '(
            "\" double quote \""
            "' single quote '"
            "( paren )"
            "{ brace }"
            "[ square ]"
            "< greater >"
            "` emacs '"
            "` markdown GRAVE ACCENT `"
            "~ tilde ~"
            "= equal ="
            "[[ double square ]]"
            "โ€œ 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 "
            )))
     (list
      (completing-read "Replace this:" xbrackets)
      (completing-read "To:" xbrackets))))
  (let (xp1 xp2 xleft xright xtoL xtoR
            (xss1 (split-string FromChars " "))
            (xss2 (split-string ToChars " ")))
    (let ((xbds (xah-get-bounds-of-block-or-region))) (setq xp1 (car xbds) xp2 (cdr xbds)))
    (setq xleft (car xss1) xright (car (last xss1)))
    (setq xtoL (car xss2) xtoR (car (last xss2)))
    (save-excursion
      (save-restriction
        (narrow-to-region xp1 xp2)
        (let ((case-fold-search nil))
          (if (string-equal xleft xright)
              (let ((xx (regexp-quote xleft)))
                (goto-char (point-min))
                (while
                    (re-search-forward
                     (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)))
            (progn
              (progn
                (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)))
              (progn
                (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))))))))))

require Emacs: xah-get-thing.el

Emacs, Work with Brackets