Bash – How to set Readline key bindings using ALT (Meta) or some other control key as prefix instead of `\C-` and `\e`

bashmacosreadlineshellterminal

I can only find examples of setting Readline key bindings using Control (\C-) or Escape (\e) as prefixes.

In my case, on macOS, the \C- space is completely filled up by default key bindings, and the \e space is not practical on a Macbook with a touchbar.

Entering "\M-f": kill-word in ~/.inputrc results in:

bind -P | grep -F "\M-"
kill-word can be found on "\e[3;5~" "\ed".

But kill-word cannot be executed using either of Option, Command, or fn as prefixes – Readline ignores it.

Is this issue specific to macOS, and how can I solve it?

Furthermore, how can I control the "timeout" that should occur before a key binding, that is a prefix of a longer key binding, is executed?

@added (@laktak): bind '"^[f": kill-word' doesn't work, but instead results in ƒ on the terminal:
enter image description here

Best Answer

I don't think you're doing anything wrong, you just need to tell Terminal.app to use the "Option" key as "Meta" instead. Just go to the preferences for Terminal (+,), then Profiles, then the Keyboard tab for your default profile and check "Use Option as Meta key."

Screenshot of Terminal.app "Profiles" → "Keyboard" settings

Otherwise, you're getting the default macOS behavior (in most keymaps) where Option + key produces special characters, just like Option+f yields the small letter "f" with hook from your screenshot (complete reference here).

In iTerm 2, I believe the left Option key already does the sensible thing by default, but just in case, here's how to change that:

iTerm 2 Option key behavior

As far as remapping Option to Escape, if that's a thing you still wanted to do, that can be accomplished from the Keyboard prefpane in System Preferences, by clicking the "Modifier Keys…" button near the bottom of the window.

macOS System Settings Keyboard prefpane

Then remap it to whatever you please (from the available choices):

Remapping modifier keys in the macOS Keyboard prefpane

I hit up this preference pane in about the first five minutes of using a new Mac, just so I can remap Caps Lock to Control, since the Control key on a Mac keyboard is in an awkward spot.

Related Question