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)?

1 Answer 1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.