如何使用Paredit转置s-expression中的(this)与(that)代码块?
How to Transpose
(this) and (that) with Paredit Great question! Paredit is built to handle exactly this kind of s-expression manipulation without breaking your balanced parentheses or forcing you to mess with manual cut/paste. Here's how to get it done:
Step-by-Step Instructions
- First, place your cursor anywhere inside either the
(this)or(that)s-expression. Paredit automatically recognizes the full boundary of the sexp, so you don't need to select the whole thing. - If you're using Emacs (the standard environment for Paredit), trigger the
paredit-transpose-sexpscommand. By default, this is bound to the key comboC-M-t(hold Control + Meta/Alt, then press t).
Example
Let's say you start with this s-expression:
(my-function (this) (that) some-argument)
After placing your cursor inside (this) and hitting C-M-t, you'll end up with:
(my-function (that) (this) some-argument)
Edge Case: Non-Adjacent Sexps
If (this) and (that) aren't right next to each other (with other code in between), you can first pull them adjacent using Paredit's slurp commands:
- Use
paredit-forward-slurp-sexp(C-M-}) to pull the next sexp into the same "group" as your current one. - Use
paredit-backward-slurp-sexp(C-M-{) to pull the previous sexp closer.
Once they're adjacent, run the transpose command as before.
The best part? Paredit keeps all your parentheses perfectly balanced the entire time—no cleanup needed afterward.
内容的提问来源于stack exchange,提问作者Dick Barends
相关产品推荐
相关产品推荐

