Delete keymap and completely disable key in zsh

keyboard shortcutszsh

I want to disable Insert key completly in zsh. This key has no real use, and only annoys me when I hit it accidentally.

I found how to remove the binding for overwrite-mode

bindkey -r "^[[2~"

But now, when I actually hit Insert, instead of activationg overwrite-mode, it prints ~ on the cursor position.

Similar thing happens when I press F12, which is not bound to anything. It also prints ~.

How can I disable the Insert key completely (and F12, as well), so that pressing it does not do anything in zsh?

I don't want to disable Insert key globally, because some other programs may actually use it for useful purpose.

Best Answer

If you press a special key (such as Insert) which sends an escape sequence that is not recognized by zsh, it will do this. zsh will try to do something with the individual bytes of the escape sequence.

Looking at the result from bindkey, it seems that zsh has no suitable function which can be bound to a key to ignore it. But you could rebind Insert to an empty macro:

bindkey -s '\e[2~' ''

Likewise for F12. Press Ctrl+V F12 to see what escape sequence F12 sends, typically

bindkey -s '\e[24~' ''

According to the zshzle manual page, there is a limitation with this approach:

As well as ZLE commands, key sequences can be bound to other strings, by using `bindkey -s'. When such a sequence is read, the replacement string is pushed back as input, and the command reading process starts again using these fake keystrokes. This input can itself invoke urther replacement strings, but in order to detect loops the process will be stopped if there are twenty such replacements without a real command being read.

Binding to \a as I originally suggested does not appear to have this drawback, i.e.,

bindkey -s '\e[2~' '\a'
Related Question