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: Fri, 20 Oct 2023 06:55:58 +0200
User-agent: Evolution 3.50.0-1

On Fri, 2023-10-20 at 01:29 +0200, Christoph Anton Mitterer wrote:
> ... then all I'd be left in need with would be a way to remove all
> bindings.
> Which seems more difficult than expected:

Maybe I found a solution:
   while IFS='' read -r keyseq; do
           bind -r "${keyseq}"
   done < <( bind -Xps | sed -En 's/^"((\\\\|\\"|[^"])+)".*/\1/p' )

Append a
   ; bind -psX | grep -v ^#
after the final closing ) in order to get the remaining binds printed
(you won't be able to do so manually, because all keys should be dead
after this).


The idea is that I blindly assumed, bash would quote only \ and "
within the first field (both with an prefixed \ ).
If that ain't the case the above would break.

The sed expression, after an anchored " subgroups any sequence of \\ or
\" or any character other than " and prints that.
It's important that the [^"] comes last in the subpattern. I think it
shouldn't matter whether \\\\ or \\" comes first.


The problems with the above:

1) I'm not even sure whether this really work in all cases, or whether
   bash does any different ways of quoting for the first field.

   I've seen that in the devel branch, where the format of bind -x may
   be one without a : after the first field, but that should still
   work.

2) Requires a subshell and even uglier, a fork to sed.

3) Requires one bind -r for each binding (doesn't seem possible to just
   specify all in one bind -r).

4) On my bash, which is Debian sid's 5.2.15 running the above causes
   one binding to remain:
   "\e\e\000": complete

   No idea where this even comes from or how one could even enter it?
   It seems to be already there in a fresh bash:

   $ bash --norc --noprofile 
   bash-5.2$ bind -Xps | grep 000
   "\e\e\000": complete


I tried my remove-all-bindings code with some simple cases:
(you may need to store these binds in a script an source it, as it
 already overwrites some characters)

a) with:
     bind -x '"\"": "echo foo"'
     bind -x '"\\": "echo bar"'
   it seems to work correctly

b) with:
     bind -x '"\"": "echo foo"'
     bind -x '"\\": "echo bar"'
     bind -x '"a b": "echo baz"'
     bind -x '"\\\\a '"'"'b\"": "echo bla"'

   Things go completely crazy:
   $ . binds.sh 
   $ bind -psX | grep 000
   "\e\e\000": complete
   "a\000": self-insert
   "\\\000": "echo bar"
   $ bind -X
   "\"": "echo foo"
   "\\\\a 'b\"": "echo bla"
   "\\\000": "echo bar"
   "a b": "echo baz"

   Err... what?!
   
   How can it come up with a "new" binding ( a\000 )?
   What is it doing to the \\ case that becomes \\\000?

   Running my unbind code leaves back:
   "\e\e\000": complete
   "a\000": self-insert
   "\\\000": "echo bar"
   

c) with only (the "\\" case removed):
     bind -x '"\"": "echo foo"'
     bind -x '"a b": "echo baz"'
     bind -x '"\\\\a '"'"'b\"": "echo bla"'

   $ . b.sh 
   $ bind -psX | grep 000
   "\e\e\000": complete
   "\\\000": self-insert
   "a\000": self-insert
   $ bind -X
   "\"": "echo foo"
   "\\\\a 'b\"": "echo bla"
   "a b": "echo baz"

   So it still adds the a\000 but the \\\000 seems indeed gone.

   My remove-all code leaves back:
   "\e\e\000": complete
   "\\\000": self-insert
   "a\000": self-insert


Any ideas, improvements or confirmations whether my code should in
principle work, would again be appreciated ;-)

Thanks,
Chris.



reply via email to

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