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:
- markdown format:
`name`
- emacs format:
`name'
- org format:
~name~
- org format:
=name=
- common template
[[name]]
- curly quote:
โnameโ
- python list:
[1,2,3]
- Wolfram Language list:
{1,2,3}
- lisp list:
(list 1 2 3)
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' Version: 2020-11-01 2023-03-31 2023-08-25 2023-09-29" (interactive (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)) (list (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))) (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)))))))))))
requires package Emacs: xah-get-thing.el ๐ฆ
Emacs, Work with Brackets
- 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 ๐ฆ