Emacs: Change Brackets ๐
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:
- python list:
[1,2,3] - Wolfram Language list:
{1,2,3} - lisp list:
(list 1 2 3) - common template
[[name]] - curly quote:
โnameโ - markdown format:
`name` - emacs format:
`name' - org format:
~name~ - org format:
=name=
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. e.g. paren ( ) french angle โน โบ double bracket [[ ]] etc. It is split by space, and last 2 items are taken as left and right brackets. ToChars is similar, with a special value of none followed by 2 spaces. ,it means replace by empty string. URL `http://xahlee.info/emacs/emacs/elisp_change_brackets.html' Created: 2020-11-01 Version: 2025-03-25" (interactive (let ((xbrackets '( "square [ ]" "brace { }" "paren ( )" "less greater than < >" "QUOTATION MARK \" \"" "APOSTROPHE ' '" "emacs ` '" "GRAVE ACCENT ` `" "double square [[ ]]" "tilde ~ ~" "equal = =" "double curly quote โ โ" "single curly quote โ โ" "french angle quote โน โบ" "french double angle ยซ ยป" "corner ใ ใ" "white corner ใ ใ" "lenticular ใ ใ" "white lenticular ใ ใ" "title 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)) (list (completing-read "Replace this:" xbrackets nil t nil nil (car xbrackets)) (completing-read "To:" xbrackets nil t nil nil (car (last xbrackets))))))) (let (xbeg xend xleft xright xtoL xtoR) (seq-setq (xbeg xend) (if (region-active-p) (list (region-beginning) (region-end)) (list (save-excursion (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point))) (save-excursion (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point)))))) (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))) (save-excursion (save-restriction (narrow-to-region xbeg xend) (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)))))))))))
Emacs, Work with Brackets
- Emacs: How to Edit Lisp Code
- Emacs Init: Highlight Brackets
- Emacs Init: Auto Insert Closing Bracket (electric-pair-mode)
- Emacs: Insert Brackets by Pair ๐
- Emacs: Delete Brackets by Pair ๐
- Emacs: Move Cursor to Bracket ๐
- Emacs: Jump to Matching Bracket ๐
- Emacs: Change Brackets ๐
- Emacs: Navigate Lisp Code as Tree
- Emacs: Select Text Between Quotes ๐
- Emacs: Xah Elisp Mode ๐ฆ