Monday, January 28, 2008

Delete Backwards Word with Option+Delete in iTerm

I just installed Leopard and after setting up option left / right arrow in iTerm, I realized that option+delete did not kill the word to the left of my cursor. I think this somehow worked in my old pre-leopard setup, although I had done nothing to set it up.

After lots of searching, I could find no easy way to map iTerm keys to backwards-delete-word because iTerm doesn't seem to support multiple key sequence bindings. There are two ways you can backwards delete a word out of the box with iterm:
Esc, Ctrl+h or Esc, Delete

Finally, using some unix trickery, I got option+delete working. First, I used the bind command to map Esc, d to backwards delete word. Edit ~/.bash_profile in your favorite text editor and add the line:

bind '"\M-d": backward-kill-word'

(Make sure you have all those quotes, otherwise it doesn't work.)

Now that you have an escape sequence that doesn't require the Ctrl key, you can map option+delete to it in iTerm.

In iTerm, go to Bookmarks > Manage Profiles. Choose Keyboard Profiles > Global and click the + button to add a key binding. Choose delete from the dropdown, check the option checkbox, and then in the Action: dropdown choose escape sequence. In the text field that appears, type d. I also checked the High interception priority checkbox for good measure.

Hit OK and you'll notice option+delete now deletes the word to the left of your cursor. Sweet!


Michael G. Noll said...

Thanks for this tip!

Michael Kuhn said...

the tcsh equivalent for the bash command is:

bindkey -b M-d backward-delete-word

Anonymous said...

By "delete key" you mean "Backspace" (for us, mortals that switched from windows)?! Also, when I hit opt+Backspace, it deletes the word to the right of the cursor, not to the left.

Is there a list of letters that can be added instead of "d" in your example?

Also, what does "Send escape sequence" really mean? From the way they represent it (^[b - for example) I'd say it's CTRL+b doesn't do the same thing as the newly assigned keyboard shortcut