# Restoring selections after accidental clear

**URL:** <https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366>\
**Category:** Help\
**Created:** [November 15, 2020, 3:04am UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366 "2020-11-15T03:04:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![EpocSquadron](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/epocsquadron/32/78_2.png) [@EpocSquadron](https://discuss.kakoune.com/u/EpocSquadron)\
**Post date:** [November 15, 2020, 3:04am UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366/1 "2020-11-15T03:04:55Z")

</div>

Often times I find myself performing a complex series of editing commands including sub selections. However, I often fumble a key and accidentally clear all of my carefully constructed selections. I know about ctrl o and saving selections with z, but the former doesn’t seem to always have the last selection state and the latter requires saving selections ahead of time (which I have done on occasion). Am i missing a convenient way to go back one step in selections? How do you all deal with this situation?

---

<div class="post-metadata">

**Author:** ![alexherbo2](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/alexherbo2/32/1140_2.png) [@alexherbo2](https://discuss.kakoune.com/u/alexherbo2)\
**Post date:** [November 15, 2020, 4:55am UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366/2 "2020-11-15T04:55:37Z")

</div>

There is currently no native solution.

You can use registers and hooks to restore the penultimate selection state.

```auto
# Save selections to the [b]ackup register.
hook -group backup-selections global NormalIdle .* %{
  set-register b %reg{z}
  execute-keys -draft '"zZ'
}

# Add a mapping to easily reach the command.
map -docstring 'Restore selections from the [b]ackup register' global user z '"bz'

```

See also the discussion [#898](https://github.com/mawww/kakoune/issues/898) to undo selection change.

---

<div class="post-metadata">

**Author:** ![almr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/almr/32/934_2.png) [@almr](https://discuss.kakoune.com/u/almr)\
**Post date:** [June 13, 2021, 3:47am UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366/3 "2021-06-13T03:47:23Z")

</div>

The `,z` map above really makes the difference between usable and unusable — at least for me as a `kak` newbie. However I wonder

- how often does the `NormalIdle` hook run: CPU cost?
- how does it work: how come the hook doesn’t run immediately after selection changes, overwriting the backup?

Further, without getting into generalized selection-undo, can I back up the selection periodically? I’d like to write a hook to save to some variable / setting every 15 seconds (perhaps with 2-3 levels of backup), but only if the selection is either multiple-region or multi-line. That would be good enough. Less than 15 seconds of “selection work”, or work that is trivial, are not worth backing up. But as it stands, `,z` breaks if you move the cursor twice.

---

<div class="post-metadata">

**Author:** ![alexherbo2](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/alexherbo2/32/1140_2.png) [@alexherbo2](https://discuss.kakoune.com/u/alexherbo2)\
**Post date:** [June 13, 2021, 8:34am UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366/4 "2021-06-13T08:34:43Z")

</div>

Normal idle runs once when you do nothing, and there is no sh call in the command, so it is wise.

---

<div class="post-metadata">

**Author:** ![alexherbo2](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/alexherbo2/32/1140_2.png) [@alexherbo2](https://discuss.kakoune.com/u/alexherbo2)\
**Post date:** [June 13, 2021, 8:41am UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366/5 "2021-06-13T08:41:04Z")

</div>

Try to execute `<c-s>` in the hook, and then use `<c-o>` for restoring.

---

<div class="post-metadata">

**Author:** ![GavinSmith0123](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/gavinsmith0123/32/1112_2.png) [@GavinSmith0123](https://discuss.kakoune.com/u/GavinSmith0123)\
**Post date:** [August 16, 2022, 10:18pm UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366/6 "2022-08-16T22:18:59Z")

</div>

I found this works for cursor motion with the keyboard but not if I accidentally moved the cursor by clicking with the mouse on the terminal window. Is there a workaround for this?

I don’t want to disable the mouse completely as I do use it in Kakoune.

I expect it is breaking because the NormalIdle hook runs twice after a mouse click.

I’m currently trying the following to go back two selections:

```auto

# Save selections to the backup registers.
hook -group backup-selections global NormalIdle .* %{
  set-register c %reg{b}
  set-register b %reg{z}
  execute-keys -draft '"zZ'
}

# Add a mapping to easily reach the command.
map -docstring 'Restore selections from the backup register' global user z ': undo-selections<ret>'

define-command undo-selections -docstring "Restore selections" %{
  execute-keys '"cz'
  set-register b %reg{c}
}

```

---

<div class="post-metadata">

**Author:** ![krobelus](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/krobelus/32/616_2.png) [@krobelus](https://discuss.kakoune.com/u/krobelus)\
**Post date:** [August 17, 2022, 8:10pm UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366/7 "2022-08-17T20:10:30Z")

</div>

> but not if I accidentally moved the cursor by clicking with the mouse on the terminal window. Is there a workaround for this?

You can try my undo-selection-change branch which adds native selection-undo.  
See [Undo selection change · Issue #898 · mawww/kakoune · GitHub](https://github.com/mawww/kakoune/issues/898#issuecomment-1207351554)

I’m pretty happy with it, will submit a PR soon.

---

<div class="post-metadata">

**Author:** ![krobelus](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/krobelus/32/616_2.png) [@krobelus](https://discuss.kakoune.com/u/krobelus)\
**Post date:** [September 10, 2022, 7:14am UTC](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366/8 "2022-09-10T07:14:57Z")

</div>

[PR is up](https://github.com/mawww/kakoune/pull/4725), I’ve been using it for a bit, it seems stable
