Restoring selections after accidental clear

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?

There is currently no native solution.

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

# 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 to undo selection change.

2 Likes