Going over start of insert action in Z shell vi-mode

vi-modezsh

Is it possible to configure the vi-mode of the Z shell so that backspace can delete characters before the position where the insert action was started?

Basically the behavior of vim which can be achieved by adding the following line to ~/.vimrc

set backspace=start

– is it possible to have this in Z shell vi-mode?

Best Answer

You can add this to your zsh configuration:

bindkey -M viins '^?' backward-delete-char
bindkey -M viins '^H' backward-delete-char

Explanation:

Vi-mode is just a set of preconfigured keymaps (viins, vicmd, viopp, visual) that bind certain keys to certain widgets. Some of these widgets are specifically designed to behave close to the way the vi editor does.

In your case this is the vi-backward-delete-char widget that is by default bound to Backspace in the viins mode, which has the specific feature to not delete past the position where insert mode was entered.

Often sligthtly different versions of these widgeds exist, that behave more in line with the emacs editor. In this case it would be the backward-delete-char widget, which is not limited to the current insert session. Although it is by default bound in emacs-mode, there are no hard restrictions on which widget can be used in which mode. You can use backward-delete-char within vi-mode by simply rebinding Backspace to call the widget backward-delete-char instead.

The reason for there being two bindings - ^? and ^H - is that it depends on the terminal, which of those two key sequences is sent on pressing Backspace. By default both are bound to the same widget in vi-mode as well as emacs-mode.

Related Question