Mac – Key binding in iTerm2

emacsitermkeybindingskeyboardterminal

I am trying to bind a shortcut to perform cut command in iTerm2. Obviously, cut is bound to Cmd + x, but I would also like it to be bound to Ctrl + w.

I did this easily for paste by going to iTerm2 → Preferences → Profiles → keys and setting the action to paste.

But there is no cut action.

So how can I achieve this?

For some other actions which don't appear listed, I have used their escape code or hex code representation as shown in answers to the question iTerm – going one word backwards and forwards.

I wonder where can I find a list of these representations and their actions?

Ideally, I would like to have Ctrl + w cut the selected text, or if there is no selection, then the entire line. Is this possible?

My goal is to make iTerm2 work more like Emacs.

Best Answer

The escape sequences mentioned in that question correspond to zle (zshell line editor) widgets documented here.

They provide emacs(or vi) like editing functions. You can see which are currently bound to keys by typing bindkey command in zsh.

You can also write shell functions and then declare those as widgets which can then be key bound. Here is a guide to zle.

The slight complication is that Iterm2 will not, by default, pass alt or Cmd modified strokes through to the shell.

This can be configured in iTerm2 → Preferences → Profiles → keys.

An alt+ key is equivalent to esc followed by key, hence to pass this to the sh you add a key bind to:

Action: Send escape sequence

Esc+: key