Emacs: Change Brackets () {} [] in Region

By Xah Lee. Date: . Last updated: .

Here's command to change brackets in text. For example, change parenthesis to square brackets, or curly brackets.

For example, here's what you have:

myMatrix =
 [
   [3, 99, 'a'],
   [2, 77, 'a'],
   [1, 77, 'a']
 ];

You want:

myMatrix =
 {
   {3, 99, 'a'},
   {2, 77, 'a'},
   {1, 77, 'a'}
 };

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

Change Bracket Pairs

Here's the code.

(require 'ido) ; bundled with emacs
(defun xah-change-bracket-pairs ( *fromType *toType *begin *end)
  "Change bracket pairs from one type to another on current line or selection.
For example, change all parenthesis () to square brackets [].

When called in lisp program, *begin *end are region begin/end position, *fromType or *toType is a string of a bracket pair. eg \"()\",  \"[]\", etc.
URL `http://ergoemacs.org/emacs/elisp_change_brackets.html'
Version 2017-01-11"
  (interactive
   (let ((-bracketsList
          '("() paren"
            "{} braces" "[] square"
            "<> greater"
            "“” curly quote"
            "‘’ single"
            "‹› french"
            "«» double french"
            "「」 corner"
            "『』 double corner"
            "【】 LENTICULAR"
            "〖〗 white LENTICULAR"
            "《》 double angle"
            "〈〉 angle "
            "〔〕 TORTOISE"
            "⦅⦆ white paren"
            "〚〛 white square"
            "⦃⦄ white braces"
            "〈〉"
            "⦑⦒"
            "⧼⧽"
            "⟦⟧ math square"
            "⟨⟩ math angle"
            "⟪⟫"
            "⟮⟯"
            "⟬⟭"
            "❛❜"
            "❝❞"
            "❨❩"
            "❪❫"
            "❴❵"
            "❬❭"
            "❮❯"
            "❰❱"
            "   none"
            )))
     (list
      (ido-completing-read "Replace this:" -bracketsList )
      (ido-completing-read "To:" -bracketsList )
      (if (use-region-p) (region-beginning) nil)
      (if (use-region-p) (region-end) nil))))
  (save-excursion
    (save-restriction
      (when (null *begin)
        (setq *begin (line-beginning-position))
        (setq *end (line-end-position)))
      (narrow-to-region *begin *end)
      (let ( (case-fold-search nil)
             (-fromLeft (substring *fromType 0 1))
             (-toLeft (if (string-equal (substring *toType 0 1) " ")
                          (progn "")
                        (substring *toType 0 1)))
             (-fromRight (substring *fromType 1 2))
             (-toRight (if (string-equal (substring *toType 1 2) " ")
                           (progn "")
                         (substring *toType 1 2))))
        (progn
          (goto-char (point-min))
          (while (search-forward -fromLeft nil t)
            (overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
            (replace-match -toLeft "FIXEDCASE" "LITERAL")))
        (progn
          (goto-char (point-min))
          (while (search-forward -fromRight nil t)
            (overlay-put (make-overlay (match-beginning 0) (match-end 0)) 'face 'highlight)
            (replace-match -toRight "FIXEDCASE" "LITERAL")))))))

Remove Quotes or Brackets

Here's a command specifically just for removing brackets.

(defun xah-remove-quotes-or-brackets (*begin *end *bracketType)
  "Remove quotes/brackets in current line or text selection.

When called in lisp program, *begin *end are region begin/end position, *bracketType is a string of a bracket pair. eg \"()\",  \"[]\", etc.
URL `http://ergoemacs.org/emacs/elisp_change_brackets.html'
Version 2017-01-11"
  (interactive
   (list
    (if (use-region-p) (region-beginning) (line-beginning-position))
    (if (use-region-p) (region-end) (line-end-position))
    (ido-completing-read
     "Replace this:"
     '("() paren"
       "{} braces" "[] square"
       "<> greater"
       "“” curly quote"
       "‘’ single"
       "‹› french"
       "«» double french"
       "「」 corner"
       "『』 double corner"
       "【】 LENTICULAR"
       "〖〗 white LENTICULAR"
       "《》 double angle"
       "〈〉 angle "
       "〔〕 TORTOISE"
       "⦅⦆ white paren"
       "〚〛 white square"
       "⦃⦄ white braces"
       "〈〉"
       "⦑⦒"
       "⧼⧽"
       "⟦⟧ math square"
       "⟨⟩ math angle"
       "⟪⟫"
       "⟮⟯"
       "⟬⟭"
       "❛❜"
       "❝❞"
       "❨❩"
       "❪❫"
       "❴❵"
       "❬❭"
       "❮❯"
       "❰❱"))))
  (save-excursion
    (save-restriction
      (narrow-to-region *begin *end)
      (let ( (case-fold-search nil))
        (mapc
         (lambda (x)
           (goto-char (point-min))
           (while (search-forward (char-to-string x)  nil t)
             (replace-match "" "FIXEDCASE" "LITERAL")))
         (substring *bracketType 0 2))))))
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs