help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to save/restore (all?) bindings


From: Christoph Anton Mitterer
Subject: Re: how to save/restore (all?) bindings
Date: Thu, 19 Oct 2023 17:11:16 +0200
User-agent: Evolution 3.50.0-1

Hey Grisha.


On Fri, 2023-10-06 at 21:54 -0400, Grisha Levit wrote:
> You can save the key bindings to arrays:
> 
> save_binds() {
>     mapfile -t save_bind < <(bind -ps)
>     mapfile -t save_bind_x < <(bind -X)
> }
> 
> Then restore the regular ones with a single `bind` command, and
> restore the
> `-x` ones with a loop:
> 
> restore_binds() {
>     local b
>     bind -- "${save_bind[@]}"
>     for b in "${save_bind_x[@]}"; do
>         bind -x "$b"
>     done
> }
> 
> This saves a subshell and a large number of `bind` invocations.

[Edit: meanwhile I noted that both yours and my version break in many
cases, see my reply to Koichi's mail on that]

Indeed, quite neat.

The only small downside is, that meanwhile I'd like to save the the
original bindings in an array element (alongside other data).
So the above solution would no longer work directly.

I guess I could store it in a local temp_array first and then store the
output of declare -p temp_array in my actual array... and later on
restore first eval the store declare command from my actual array to
get the temp array back... and then use that.

But that may be even slower (didn't test it though) and ugly to read.


It also turns out that I only need to change three bindings that I also
want/need to restore:

   1. bind '"\C-m": accept-line'
      => to accept my cd command and have the prompt re-evaluated
         (yeah, unlikely someone changed this binding, but who knows)
   
   2. bind -x '"\C-j": __someRestoreFunction'
      => to trigger the restore of the bindings and the READLINE_* vars
      That binding I could even save by using the one from my main
      function and restoring it later.
   
   3. bind '"\e[0n": "\C-m\C-j"'
      => to trigger of the (1) and (2) from my "main" function by
         printf-ing '\e[5n'
   
   Could of course use other keyseqs... but since I restore them
   anyway.
   Though I do wonder whether there are some subtle effects which I
   don't know.

But I don't see a way how this could be used for any simplification or
to only store/restore those three.


> You can
> still do the latter with your `eval` method of you like, though it's
> harder
> to read imo.

At least, AFAIU, your method does conceptually the same for
storing/restoring the data.

Though I escape with printf's %q because I use the eval... does this
mess anything up?


> If you want to restore any unbound key sequences back to an unbound
> state,
> you'll want to also first `bind -r` any key sequences that you have
> assigned before doing the restore.

Oh, thanks for catching that. I'd have overseen that.


>   Note that `bind -r` removes any
> binding, it does not restore a default as you suggest.

Yeah, thanks as well, though I've had noted that myself meanwhile ^^



Cheers & thanks again,
Chris.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]