# DrRacket suggestion: Add a hotkey to peel off the most immediate surrounding s-expression

**URL:** <https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057>\
**Category:** General\
**Tags:** drracket, suggestion\
**Created:** [June 4, 2022, 5:53pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057 "2022-06-04T17:53:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse0x42](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@Jesse0x42](https://racket.discourse.group/u/Jesse0x42)\
**Post date:** [June 4, 2022, 5:53pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057/1 "2022-06-04T17:53:11Z")

</div>

DrRacket already makes it easy to delete or hide an entire s-expression (i.e. parentheses bounded expression).

However, often you actually want to delete only the surrounding s-expression rather than the entire expression. It would be nice if there was a built-in hotkey in DrRacket to do this whenever you are on or in an s-expression.

For example, if applied to `(+ (- a b) c)` while inside `(- a b)` it would change to `(- a b)`, deleting only the `(+ ... c)` part.

That would be super convenient and useful.

It is a very common operation when editing any Lisp/Scheme-like languages.

PS: I really appreciate how elegant and cleaning designed Racket is and how abundant and easy to get working its libraries are. This is my first post on Racket's Discourse page by the way (if my memory is correct).

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [June 4, 2022, 11:07pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057/2 "2022-06-04T23:07:43Z")

</div>

You can create a custom keyboard binding, following [https://docs.racket-lang.org/drracket/Keyboard\_Shortcuts.html](https://docs.racket-lang.org/drracket/Keyboard_Shortcuts.html)

For your use case:

1. Create a new Racket file with the following content:

```scheme
#lang s-exp framework/keybinding-lang

(define (rebind key . commands)
  (keybinding
   key
   (λ (ed evt)
     (send ed begin-edit-sequence)
     (for-each (λ (command)
                 (send (send ed get-keymap) call-function
                       command ed evt #t))
               commands)
     (send ed end-edit-sequence))))

(rebind "c:w"
        "up-sexp"
        "select-forward-sexp"
        "cut-clipboard"
        "up-sexp"
        "select-forward-sexp"
        "paste-clipboard")

```

1. Enter “Edit \> Keybindings \> Add User-defined Keybindings..”. Choose the Racket file that you just created.

2. Restart DrRacket

Here’s an example interaction, where `|` indicates the caret:

```scheme
(* (+ (- a |b) c) d)

;; press c-w

(* (- a b)| d)

```

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [June 5, 2022, 3:00pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057/3 "2022-06-05T15:00:48Z")

</div>

In the language of [GitHub - benknoble/vim-sexp: Precision Editing for S-expressions](https://github.com/benknoble/vim-sexp) which borrows from the original [GitHub - guns/vim-sexp: Precision Editing for S-expressions](https://github.com/guns/vim-sexp), which was probably inspired by ParEdit or similar, this would be called "raising" the s-expression.

---

<div class="post-metadata">

**Author:** ![Jesse0x42](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@Jesse0x42](https://racket.discourse.group/u/Jesse0x42)\
**Post date:** [June 5, 2022, 9:48pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057/4 "2022-06-05T21:48:34Z")

</div>

Thank you guys so much [sorawee](https://racket.discourse.group/u/sorawee) and [benknoble](https://racket.discourse.group/u/benknoble)!

I'll definitely try out both of these next time I get the time to. I wasn't expecting someone to give me an implementation so fast especially!

I'm a Vim user too by the way, although for Racket I haven't been using Vim for much more than larger scale substitutions when working with Racket so far since the indentation rules and other formatting nuances are so different from most languages. Sometimes I'll do a transformation in Vim and then reload in DrRacket currently, basically. It's good to know there's related tools and it's nice to have a name for the operation too.

Sorry for the delay in my response. I've been busy with random things.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [June 6, 2022, 9:01pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057/5 "2022-06-06T21:01:10Z")

</div>

RE: vim, you may be interested in the recently-revised [24.3&nbsp;Vim](https://docs.racket-lang.org/guide/Vim.html). I would be happy to share more about my setup, too.

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [June 8, 2022, 11:11pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057/6 "2022-06-08T23:11:41Z")

</div>

It doesn't look to me like anyone here has mentioned C:c,C:o, which I use all the time to accomplish this. It specifically discards the s-expression surrounding the one that the cursor is immediately before. So, in your example, given the expression `(+ (- a b) c)`, if the cursor was just before the `(- a b)`, then hitting C:c,C:o would discard the `(-` and the `c)`. If (as you mention) you want to do this when the cursor is inside of the `(- a b)`, you would use alt-up-arrow first.

To add to this, there's also C:c,C:e, which deletes the outer parens without deleting any of the content—a "splice", if you will. Using C:c,C:e instead of C:c,C:o would leave you with `+ (- a b) c`. This is very useful when e.g. you want to unwrap an expression and rearrange its elements.

---

<div class="post-metadata">

**Author:** ![hendrikboom3](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/hendrikboom3/32/2748_2.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)\
**Post date:** [June 9, 2022, 2:50pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057/7 "2022-06-09T14:50:29Z")

</div>

> [@jbclements](#):
>
> given the expression `(+ (- a b) c)`, if the cursor was just before the `(- a b)`, then hitting C:c,C:o would discard the `(-` and the `c)`

I'm guessing that there's a typo and you meant to discard the '(+' instead of the '(-'.

---

<div class="post-metadata">

**Author:** ![Jesse0x42](https://avatars.discourse-cdn.com/v4/letter/j/87869e/32.png) [@Jesse0x42](https://racket.discourse.group/u/Jesse0x42)\
**Post date:** [June 10, 2022, 9:06pm UTC](https://racket.discourse.group/t/drracket-suggestion-add-a-hotkey-to-peel-off-the-most-immediate-surrounding-s-expression/1057/8 "2022-06-10T21:06:20Z")

</div>

Hello again everyone!

I'm just stopping by to say thanks again for all this wonderful help you've all provided!

Racket's elegant design is very pleasant and getting more acquainted with manipulating s-expressions more flexibly and easily like this should help even more.

Having this thread here will also help other users searching the internet to figure out various ways of manipulating Racket's parenthetical expressions more easily too, so that's a bonus as well.

Anyway, looking forward to experimenting with Racket more in the future. 🕶
