# Is there a way to get DrRacket to indent all on save?

**URL:** <https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009>\
**Category:** Questions & Answers\
**Tags:** drracket\
**Created:** [June 13, 2023, 7:27pm UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009 "2023-06-13T19:27:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![madkins23](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/madkins23/32/1208_2.png) [@madkins23](https://racket.discourse.group/u/madkins23)\
**Post date:** [June 13, 2023, 7:27pm UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/1 "2023-06-13T19:27:30Z")

</div>

That's about it. I would like formatting to be done automagically when a definition file is saved.

I figure it must be possible, but may be require a script or plugin. I can't find a preference or setting that would do it.

---

<div class="post-metadata">

**Author:** ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)\
**Post date:** [June 14, 2023, 4:54pm UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/2 "2023-06-14T16:54:02Z")

</div>

I think it’s fair to say that this is not something DrRacket does out of the box.

I see the value.

This might be something that can be done in a plug-in or as a change to an existing plugin.

[https://pkgd.racket-lang.org/pkgn/search?q=plugin+&tags=](https://pkgd.racket-lang.org/pkgn/search?q=plugin+&tags=)

S.

Now I’m wondering if DrRacket can reformat on each keypress, in the same way that it automatically balances parentheses.

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [June 15, 2023, 2:34am UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/3 "2023-06-15T02:34:05Z")

</div>

I have a quickscript sript that removes trailing whitespace from lines and then saves the file (There's currently no way to hook scripts to run on particular actions like saving). You might be able to use it as a starting point for using with the formatter/pretty printer of your choice.

[http://pasterack.org/pastes/476](http://pasterack.org/pastes/476)

---

<div class="post-metadata">

**Author:** ![madkins23](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/madkins23/32/1208_2.png) [@madkins23](https://racket.discourse.group/u/madkins23)\
**Post date:** [June 15, 2023, 12:49pm UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/4 "2023-06-15T12:49:47Z")

</div>

Thanks for the script.

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [June 19, 2023, 9:02pm UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/5 "2023-06-19T21:02:31Z")

</div>

> [@shawnw](#):
>
> There's currently no way to hook scripts to run on particular actions like saving

Working on it 🙂 No idea when this will be ready though.

---

<div class="post-metadata">

**Author:** ![madkins23](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/madkins23/32/1208_2.png) [@madkins23](https://racket.discourse.group/u/madkins23)\
**Post date:** [June 19, 2023, 10:13pm UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/6 "2023-06-19T22:13:50Z")

</div>

Are you working on a DrRacket change or is there a way to do it with a plugin?

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [June 20, 2023, 6:42am UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/7 "2023-06-20T06:42:46Z")

</div>

I'm working on upgrading Quickscript to have hooks that trigger on certain events.

And yes, you can do what you want with a DrRacket plugin, by augmenting [on-save-file](https://docs.racket-lang.org/gui/editor___.html#%28meth._%28%28%28lib._mred%2Fmain..rkt%29._editor~3c~25~3e%29._on-save-file%29%29) by extending the definitions text% with [drracket:get/extend:extend-definitions-text](https://docs.racket-lang.org/tools/drracket_get_extend.html#%28def._%28%28lib._drracket%2Ftool-lib..rkt%29._drracket~3aget%2Fextend~3aextend-definitions-text%29%29). See a plugin example in the [docs](https://docs.racket-lang.org/tools/implementing-tools.html).

(The purpose for the new hook system in Quickscript is to simplify the process of writing such plugins.)

---

<div class="post-metadata">

**Author:** ![madkins23](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/madkins23/32/1208_2.png) [@madkins23](https://racket.discourse.group/u/madkins23)\
**Post date:** [June 20, 2023, 11:39am UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/8 "2023-06-20T11:39:25Z")

</div>

The first time I looked at that page it went right over my head. Thanks for making me look at it again. The magic of Lisp.

Adding hooks to Quickscript sounds like a great idea. If there's any way I can help let me know.

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [September 10, 2023, 3:25pm UTC](https://racket.discourse.group/t/is-there-a-way-to-get-drracket-to-indent-all-on-save/2009/9 "2023-09-10T15:25:08Z")

</div>

Now that Quickscript has hooks (see [here](https://racket.discourse.group/t/any-way-to-hook-running-a-quickscript-script-on-events-in-dr-racket/1887/6)), it should be fairly easy to format as you wish:

From [this script](https://gist.github.com/Metaxal/2628c2d4909cee135e5f6dd8254602a0), just replace the line that removes trailing spaces with

```scheme
       (define new-str (program-format str))

```

This requires Racket ≥v8.10.3 (which is in the snapshot for now). Let me know if that works — or not!
