MacOS – How to customize .inputrc to make Command-Delete and Option-Delete delete lines and words in iTerm 2

itermkeyboardmacos

I'd like to add a little bit of consistency between editing text in the terminal and editing text in normal OS X programs. I use iTerm2, but I imagine any solution would involve using readline and .inputrc which probably would work in most terminals.

Specifically, I was thinking of adding:

  • CommandDelete: Delete to start of line before cursor
  • OptionDelete: Delete word before cursor
  • OptionLeft/Right Arrow: Move cursor between words
  • CommandLeft/Right Arrow: Move cursor to start/end of line

Questions

  • How could .inputrc be configured to support these keyboard shortcuts?
  • Would any of these shortcuts generate conflicts with existing terminal or command-line shortcuts?
  • Are there any other shortcuts that would be useful to create consistency with standard OS X text editing keys?

As a side note, I am aware that there are Vi and Emacs style shortcuts for performing these operations. In fact I use Vi-style shortcuts quite a lot on the command-line. However, sometimes when my mind hasn't shifted modes, it’s disruptive. Thus, it would be nice if the terminal could support both OS X and Vim style shortcuts.

Best Answer

⌘← can be assigned to \eOH and ⌘→ to \eOF. You could assign ⌥← to \eb like in Terminal, but it doesn't work in vi mode or nano.

In emacs mode you could also add this to .inputrc:

"\e[1;9D": backward-word
"\e[1;9C": forward-word
"\e[3~": kill-word

C-v shows the escape sequence for the next key combination. ^[ is ESC in caret notation. bind -p prints currently bound commands.