Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Make key mappings work in bash UNIX
there is a strange behaviour in [at least my] which prevented me from tuning bash to my preferences for some time now. In Terminal -> Window Settings (i.e. the Terminal Inspector window) -> Keyboard section, the Key Mappings section contained these two entries (among others):
  • control cursor left: [5D
  • control cursor right: [5C
For some reason, the Terminal would not send these keycodes to readline and thus to bash, though. After looking for the mistake in a lot of other places, I just changed the entries in the Terminal Inspector to exactly the same values as they already were, and the desired key mapping worked instantly.

Key mapping is quite useful for utilizing various bash convenience features. For example, to be able to move the cursor in bash one word at a time, first make the changes described above (re-typing exactly what was already there), then add these two lines to your ~/.inputrc file:
"[5D": backward-word
"[5C": forward-word
Since I patched Panther over Jaguar and Jaguar over Puma, this problem could be non-existent on other systems, of course.

[robg adds: I had the exact same issue on my machine, though I had to add the lines to the .inputrc file first -- but even after doing that, the control-left and control-right arrows didn't move a word at a time until I re-typed the key definitions. Odd!]
  • Currently 3.00 / 5
  You rated: 1 / 5 (4 votes cast)

Make key mappings work in bash | 9 comments | Create New Account
Click here to return to the 'Make key mappings work in bash' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
dupe :)
Authored by: folkert on Aug 06, '04 10:28:44AM
actually, this hint is a little outdated, since i submitted a more detailed version in january :)

look at it here:
older hint
or on my page [this time with correctly escaped code tags]


$ hexdump /mach_kernel|head -1
0000000 feed face 0000 0012 0000 0000 0000 0002

[ Reply to This | # ]

What about end of line, beginning of line, etc?
Authored by: the_webmaestro on Aug 06, '04 11:13:12AM

Are there keyboard mappings for the Home Key, End Key, Page Up & Page Down?

Father of Jeremy Logan Leeds

[ Reply to This | # ]
What about end of line, beginning of line, etc?
Authored by: gsfgf on Aug 06, '04 09:22:20PM

just hold down shift when pressing them

[ Reply to This | # ]
Make key mappings work in bash
Authored by: kwyjibo on Aug 06, '04 12:11:56PM
You can still move the cursor forward or backward one word at a time without these key bindings. If your bash is set to emacs mode (the default) you can use ESC-f and ESC-b which, admittedly, is a little more cumbersome than using the control left/right arrow combination. I actually prefer to use \C-f and \C-b as my forward/backward word commands, since these mirror my bindings in emacs. If you are a vi user, you can enable vi mode by adding set -o vi to your .bash_profile. Then you can use vi cursor motion commands instead.

[ Reply to This | # ]
Make key mappings work in bash
Authored by: MattHaffner on Aug 06, '04 12:19:21PM

Even easier, just turn on "Use option key as meta key" and then Opt-F and Opt-B work as Meta-F and Meta-B (and so on for every other key...). The default Emacs bindings for M-F and M-B are forward and backward word motion.

[ Reply to This | # ]
Make key mappings work in bash
Authored by: osxpounder on Aug 09, '04 05:27:57PM

Hmm, the Esc-f and Esc-b worked for me in iTerm, but when I told iTerm's prefs to treat the Option key as a meta-key, nothing changed, even after restarting iTerm.


[ Reply to This | # ]
Make key mappings work in bash
Authored by: koelling on Aug 07, '04 02:36:08AM

Does anyone know a way to make this work in iTerm? There are keyboard profiles to choose from but no way that I can tell to add to them. Also, props to the poster above who mentioned set -o vi. I had not known that and it is the best thing I've heard all day.

[ Reply to This | # ]
Can no longer type [
Authored by: kaih on Aug 08, '04 08:55:39AM
I've applied these suggestions (creating ~/.inputrc with the suggested content) and editing the Window Settings in Terminal, to re-enter what was already there. What happens now is that I can't type the [ character at all... If I hit the [ key, the shell seems to ignore it, and the following character (are they being interpreted weirdly by readline?) and if I hit [ twice, I still don't get even a single [ character...
I do have the occasional use for [ on the command line - I wouldn't like to have to give up, say, entering ranges to download with curl like:
curl -O[1-4].tar.gz
Does anyone else have this problem, or know what I can do about it?


[ Reply to This | # ]

Escape is invisible
Authored by: thinkyhead on Aug 09, '04 05:29:51PM

This hint is completely wrong.

In the Terminal's "Keyboard" section of the "Window Settings..." dialog you can enter the ESC character by pressing option-escape. However escape doesn't show up as a printable character. All this hint does is replace escape-sequences with bindings using the bracket character, which unfortunately prevents typing the bracket character.

Instead, enter your keybindings with ESC included and use the original .inputrc or escaped bindings to get the behavior you want, as outlined in the earlier article mentioned in the first reply.

| slur was here

[ Reply to This | # ]