1

If I put "\M-\C-k": kill-whole-line in my ~/.inputrc file the result is "\213": kill-whole-line (displayed by bind -p).

However, if I change \M- to \e ("\e\C-k": kill-whole-line), the key binding works as expected.

How can I use \M-\C- ("meta-control") as a prefix, when defining key bindings using Readline in bash?

0

1 Answer 1

4

You can't use \M- because it's broken. Keep using \e for the Meta/Alt prefix.

Using \M- in readline bindings does not work reliably with terminal emulators which prepend an ESC (\x1b) character instead of turning on the high bit when Meta/Alt is pressed and/or with UTF-8 locales (both of which are generally the default on most modern systems [1]).

With some versions of bash (e.g. 5.0, but not the newer 5.1), using \C-\M- works differently from \M-\C-, so just using \C-\M-k instead of \M-\C-k may actually do what you want -- until the next update ;-)

bash-5.0$ bind '"\C-\M-k": shell-backward-kill-word'
bash-5.0$ bind '"\M-\C-k": shell-kill-word'
bash-5.0$ bind -p | grep -a shell-.*kill-word
"\e\C-k": shell-backward-kill-word
"\213": shell-kill-word

[1]: not with xterm though, where you would need something like XTerm*VT100.metaSendsEscape: true in your ~/.Xresources.

3
  • thank you. By "broken" you mean Readline is "bugged", or do you mean that I'm attempting something that's not right?
    – Shuzheng
    Commented Jan 29, 2021 at 10:19
  • It's readline which is broken. Or at least its documentation -- they should clearly document that \M- is only usable with terminal emulators which are configured to use legacy 8-bit (non utf-8) encodings and to turn the high bit instead of prepending an ESC when Alt is pressed. But there are zero reasons to use that config nowadays, and it's not the default anywhere.
    – user313992
    Commented Jan 29, 2021 at 10:40
  • Thank you so much for the explanation. Lots of frustration over here too :)
    – ychaouche
    Commented Apr 28, 2022 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .