Emacs: Change Brackets/Quotes ๐
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:
`function`
- emacs format:
`function'
- org format:
~function~
- org format:
=function=
- common template
[[function]]
- curly quote:
โfunctionโ
- python list:
[1,2,3]
- Wolfram Language list:
{1,2,3}
- lisp list:
(list 1 2 3)
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
- Emacs: Highlight Brackets
- Emacs: Insert Bracket Pairs, 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/Quotes ๐
- Emacs: Navigate Lisp Code as Tree
- Emacs: Select Line, between Quotes, Extend Selection ๐
- Emacs: Xah Elisp Mode