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 ( *from-chars *to-chars)
  "Change bracket pairs from one type to another on current line or text selection.
For example, change all parenthesis () to square brackets [].

When called in lisp program, *from-chars or *to-chars is a string of bracket pair. eg \"()\",  \"[]\", etc. If the string has length greater than 2, the rest are ignored.
URL `http://ergoemacs.org/emacs/elisp_change_brackets.html'
Version 2017-03-02"
  (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 ))))

  (let ( -begin -end )
    (if (use-region-p)
        (progn (setq -begin (region-beginning)) -end (region-end))
      (progn (setq -begin (line-beginning-position) -end (line-end-position))))
    (save-excursion
      (save-restriction
        (narrow-to-region -begin -end)
        (let ( (case-fold-search nil)
               (-fromLeft (substring *from-chars 0 1))
               (-toLeft (if (string-equal (substring *to-chars 0 1) " ")
                            (progn "")
                          (substring *to-chars 0 1)))
               (-fromRight (substring *from-chars 1 2))
               (-toRight (if (string-equal (substring *to-chars 1 2) " ")
                             (progn "")
                           (substring *to-chars 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 (*bracket-chars)
  "Remove quotes/brackets in current line or text selection.

When called in lisp program, *begin *end are region begin/end position, *bracket-chars is a string of a bracket pair. eg \"()\",  \"[]\", etc.
URL `http://ergoemacs.org/emacs/elisp_change_brackets.html'
Version 2017-03-03"
  (interactive
   (list
    (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"
       "⟪⟫"
       "⟮⟯"
       "⟬⟭"
       "❛❜"
       "❝❞"
       "❨❩"
       "❪❫"
       "❴❵"
       "❬❭"
       "❮❯"
       "❰❱"))))
  (let (-begin -end)
    (if (use-region-p)
        (progn (setq -begin (region-beginning) -end (region-end)))
      (progn (setq -begin (line-beginning-position) -end (line-end-position))))
    (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 *bracket-chars 0 2)))))))

Text Transform Topic

  1. Emacs: Toggle Letter Case
  2. Emacs: Change to Title Case
  3. Emacs: Upcase Sentences
  4. Emacs: Cycle Replace Space Hyphen Underscore
  5. Emacs: Remove Accent Marks
  6. Emacs: Escape Quotes Command
  7. Emacs: Quote Lines
  8. Emacs: Change Brackets () {} [] in Region
  9. Emacs: CSS Compressor
  10. Emacs: Replace Greek Letter Names to Unicode
  11. Emacs: Convert Straight/Curly Quotes
  12. Emacs: Convert English/Chinese Punctuations
  13. Emacs: Lines to HTML Table
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs