Patch-ID: bash41-003
[bash.git] / examples / scripts.noah / remap_keys.bash
blobaa7c463d158ab1e08c131721817ea6352f3544b1
1 # remap_keybindings.bash
2 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
3 # Created: 1992-01-11
4 # Last modified: 1993-02-03
5 # Public domain
7 # Conversion to bash v2 syntax done by Chet Ramey
9 # Commentary:
10 # Code:
12 #:docstring remap_keybindings:
13 # Usage: remap_keybindings old_function new_function
15 # Clear all readline keybindings associated with OLD_FUNCTION (a Readline
16 # function) rebinding them to NEW_FUNCTION (`self-insert' by default)
18 # This requires bash version 1.10 or newer, since previous versions did not
19 # implement the `bind' builtin.
20 #:end docstring:
22 ###;;;autoload
23 function remap_keybindings ()
25 local unbind_function="$1"
26 local bind_function="${2:-'self-insert'}"
27 local bind_output
28 local arg
30 # If they're the same thing, the work has already been done. :-)
31 if [ "${unbind_function}" = "${bind_function}" ]; then
32 return 0
35 while : ; do
36 bind_output="$(bind -q ${unbind_function} 2> /dev/null)"
38 case "${bind_output}" in
39 "${unbind_function} can be invoked via"* ) ;;
40 "" ) return 1 ;; # probably bad argument to bind
41 *) return 0 ;; # unbound
42 esac
44 # Format of bind_output is like:
45 # 'quoted-insert can be invoked via "\C-q", "\C-v".'
46 # 'self-insert can be invoked via " ", "!", """, "$", "%", ...'
47 set -- ${bind_output}
48 shift 5
50 for arg in "$@" ; do
51 # strip off trailing `.' or `,'
52 arg=${arg%.};
53 arg=${arg%,};
55 case ${arg} in
56 ..)
57 # bind -q didn't provide whole list of key bindings; jump
58 # to top loop to get more
59 continue 2 ;
62 bind "${arg}: ${bind_function}"
64 esac
65 done
66 done
69 provide remap_keybindings
71 # remap_keybindings.bash ends here