OS X Terminal: "backward delete word" works only with C-M-h

Followup on Mac OS X Terminal: Map option+delete to “backward delete word”.

The Use option key as meta key option is checked, but the deleting word backwards works only with ^--H (i.e. one has to also hold the ^ or Control key)! How can you backward delete a word with -H only? This annoyance happens only in Terminal.app ... and interestingly also in iTerm.app!

I also have this in ~/Library/KeyBindings/DefaultKeyBinding.dict, so that I can enjoy Emacs throughout:

{"~h" = "deleteWordBackward:";}

Environment: OS X 10.8.2 (12C54), Terminal 2.3 (309), iTerm


Solution 1:

Settings in DefaultKeyBinding.dict don't apply to the shell views in Terminal or iTerm 2.

You can make M-h delete a word backward by adding this to ~/.inputrc

"\eh": backward-kill-word

M-h is bound to mark-paragraph by default in Emacs though.

If others search for how to make ⌥⌫ delete a word backward, just enable using option as a meta key:

  • In Terminal, check Settings > (your profile) > Keyboard > Use option as meta key
  • In iTerm 2, set the option keys to act as "+ Esc" in Profiles > (your profile) > Keys

If others search for how to make ⌥⌦ delete a word forward, you can make it insert \ed:

⌥⌦ inserts \e( by default in Terminal and \e[3~ in iTerm 2, so you could also add "\e(": kill-word or "\e[3~": kill-word to ~/.inputrc.

Solution 2:

Almost all terminal use emacs hotkeys to manipulate text (see readline keyboard shortcuts), and in emacs, you use backward-kill-word (meta-delete) to delete the previous word.

And, in emacs, meta-<KEY> (key composition) equals to escape <KEY> (key chord), so in this case, you can use ESCAPE DEL to delete the previous word.