8

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?

3
  • Possible? I'm not sure, but if nothing else, the source code is there. Note though that you're dealing with "vi-mode", not "vim-mode", so it is quite likely that several vim-exclusives are missing
    – Fox
    Commented May 31, 2017 at 3:17
  • @Fox if you know a patch, feel free to post it :-)
    – viuser
    Commented May 31, 2017 at 3:25
  • 1
    This doesn't require a patch. Even if it can't be done with configuration, it can be done by defining a function that does the job and binding it to backspace. Commented May 31, 2017 at 22:28

1 Answer 1

12

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.

1
  • I spent a long time trying to figure this one out. Thanks!
    – PeterM
    Commented Nov 30, 2021 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .