# Small patch of autowrap.kak to preserve indentation

**URL:** <https://discuss.kakoune.com/t/small-patch-of-autowrap-kak-to-preserve-indentation/2711>\
**Category:** Recipes & Guides\
**Created:** [June 23, 2025, 6:45pm UTC](https://discuss.kakoune.com/t/small-patch-of-autowrap-kak-to-preserve-indentation/2711 "2025-06-23T18:45:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ficd](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/ficd/32/1560_2.png) [@ficd](https://discuss.kakoune.com/u/ficd)\
**Post date:** [June 23, 2025, 6:45pm UTC](https://discuss.kakoune.com/t/small-patch-of-autowrap-kak-to-preserve-indentation/2711/1 "2025-06-23T18:45:11Z")

</div>

Exactly what it says on the tin. By changing one line in `autowrap.kak`, the autowrapped lines will have the same indentation as the line above. I’ve included the patch below if you want to apply it.

```diff
--- /usr/share/kak/rc/tools/autowrap.kak	2025-06-13 16:08:11.000000000 -0400
+++ /home/fic/.config/kak/autoload/autowrap.kak	2025-06-23 14:39:54.592580870 -0400
@@ -19,7 +19,8 @@
             reg m "%val{selections_desc}"
 
             ## if we're adding characters past the limit, just wrap them around
- execute-keys -draft "<a-h><a-k>.{%opt{autowrap_column}}\h*[^\s]*<ret>1s(\h+)[^\h]*\z<ret>c<ret>"
+ ## PATCH: added <esc>ghH<a-&> to preserve prev line indent when wrapping.
+ execute-keys -draft "<a-h><a-k>.{%opt{autowrap_column}}\h*[^\s]*<ret>1s(\h+)[^\h]*\z<ret>c<ret><esc>ghH<a-&>"
         } catch %{
             ## if we're adding characters in the middle of a sentence, use
             ## the `fmtcmd` command to wrap the entire paragraph
@@ -48,3 +49,4 @@
 define-command autowrap-disable -docstring "Disable automatic line wrapping" %{
     remove-hooks window autowrap
 }
+

```
