1

I rely on toggle-quotes (to switch between single and double quotes), but I'm not finding any available packages that can do the same for switching between pairs of ( [ { <. I'd expect it to cycle between the four (and maybe /). Does it exist (yet)?

Drew
  • 75,699
  • 9
  • 109
  • 225
Micah Elliott
  • 221
  • 3
  • 6

1 Answers1

1

This is a simplistic way to accomplish it for lisp modes (for the limited cases I just tried), using some smartparens functions:

(defun toggle-parens ()
  "Toggle parens, braces, brackets."
  (interactive)
  (save-excursion
    (when (not (string-match-p (regexp-quote (char-to-string (char-after))) "([{<"))
      (sp-backward-up-sexp)
      (when (eq ?\" (char-after)) ; up again if inside string
    (sp-backward-up-sexp)))
    (progn
      (sp-wrap-with-pair
       (case (char-after)
         (?\( "[")
         (?\[ "{")
         (?\{ "(")
         ;; smartparens can't wrap with <
         ;; (?\< "(")
       ))
      (forward-char)
      (sp-splice-sexp))))
(global-set-key (kbd "C-c S") 'toggle-parens)

Micah Elliott
  • 221
  • 3
  • 6