# Create backup of each edited file

**URL:** <https://discuss.kakoune.com/t/create-backup-of-each-edited-file/1016>\
**Category:** Plugins\
**Tags:** plugins\
**Created:** [May 14, 2020, 5:54pm UTC](https://discuss.kakoune.com/t/create-backup-of-each-edited-file/1016 "2020-05-14T17:54:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmerejkowsky](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/dmerejkowsky/32/696_2.png) [@dmerejkowsky](https://discuss.kakoune.com/u/dmerejkowsky)\
**Post date:** [May 14, 2020, 5:54pm UTC](https://discuss.kakoune.com/t/create-backup-of-each-edited-file/1016/1 "2020-05-14T17:54:24Z")

</div>

# Why?

I wrote this plugin after having lost a few hours re-writing a file I had deleted by accident (yes I was in a git repo, but I ran `git clean -f` by accident - it happens)

I thought about several solutions and came up with this one, that you may find interesting.

# Usage

- Download `kak-back` from [my dotfiles repo](https://github.com/dmerejkowsky/dotfiles/blob/master/dm-tools/bin/kak-back) - it’s a standalone Python3 script.
- Make it executable and put it somewhere in your PATH
- Call `kak-back backup` in a WritePost hook:

```auto
hook global BufWritePost .* %{ 
  nop %sh{ kak-back backup "${kak_hook_param}" } 
}

```

- Restore a deleted file with : `kak-back restore PATH`

Remember to run `kak-back clean` from time to time, otherwise `~/.local/share/kak/backups` will grow indefinitely.

If you find this interesting, I may take the time to convert it to a proper kak plugin instead - but in its current form it’s easier to tweak should you have to.

Cheers!

---

<div class="post-metadata">

**Author:** ![duncan](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/duncan/32/95_2.png) [@duncan](https://discuss.kakoune.com/u/duncan)\
**Post date:** [May 15, 2020, 11:09pm UTC](https://discuss.kakoune.com/t/create-backup-of-each-edited-file/1016/2 "2020-05-15T23:09:58Z")

</div>

Quick link to [Default hooks](https://github.com/mawww/kakoune/blob/master/doc/pages/hooks.asciidoc#default-hooks).

```auto
hook global KakEnd .* %{
 nop %sh{ kak-back clean }
}

```

As the clean function checks `if now - mtime > ONE_MONTH`

Cool stuff, going to try it out. Thanks.

* * *

Had a play and:

- the obligatory user knows best: ONE\_MONTH into command line flag
- for easier rollback on incremental saves of the same file a date-time suffix: 2020-05-16-12:27:14.509998
- changed directory share/kak to share/kak-back inline with your share/kak-spell
- just a couple of tests for kak-back clean and all good.

`$ cat kak-back.py > kak-back && chmod u+x,u-w,og-rwx kak-back && mv kak-back ~/local/bin/`

And cool, cool, do your thing.

* * *

would also be nice if it had a command line flag `--ignore-dot` for private dot files.
