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: Grisha Levit
Subject: Re: how to save/restore (all?) bindings
Date: Fri, 6 Oct 2023 21:54:07 -0400

On Sun, Oct 1, 2023, 20:40 Christoph Anton Mitterer <calestyo@scientia.org>
wrote:

> I'd be happy to get any feedback on my function:
> - improvements (performance, functionality)
> - whether it's all wrong or I miss saving some statuses or could be
>   done simpler
> - or whether I've overseen any pitfalls in quoting, using %q, IFS and
>   so on
> - etc.
>

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. You can
still do the latter with your `eval` method of you like, though it's harder
to read imo.

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.  Note that `bind -r` removes any
binding, it does not restore a default as you suggest.


reply via email to

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