You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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-sexps command. By default, this is bound to the key combo C-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:13:51