Why doesn't the backspace key work in insert mode?
Trying to delete characters in insert mode with the backspace key sometimes doesn't seem to work. I can backspace sometimes, but at other times it does nothing; the cursor doesn't go to the left, and absolutely nothing seems to happen.
I noticed this using gVim in Windows. The backspace seems to work as expected while using Vim from the terminal in most Linux systems however.
- Why does this happen?
- How can I make the backspace key delete characters as usual?
- Is this behaviour intended as a feature? In other words: are there better alternatives to the backspace to delete characters in insert mode?
While the answer below correctly solves your problem, I've found that disabling keys such as arrow keys or backspace help you get used to doing things more properly. Backspace in vim is done with `X` (and delete with `x`), but often what you really wanted to do was `db` for example to delete until the beginning of the word, or `daw` to delete the whole word etc. If you have backspace available, you risk using this suboptimal key in more cases than necessary.
@Shahbaz I disagree: While in insert mode it makes sense to be able to make minor corrections using the backspace key. I agree that one should learn how to use the normal mode commands, but it is not inproper to use backspace while in insert mode for minor corrections.
@KarlYngveLervåg, in the case of backspace I agree (actually I don't have it disabled myself). However, it looks like the OP has the default behavior of backspace which doesn't work for example with start of line, but works within the line. Still, the OP has better options than backspace. For example, instead of backspacing to the previous line, one can use `J` which takes care of the whitespaces as well. In short, backspace is fine for quick typo fixes during writing, but other than that there are better alternatives.
The problem of many people isn't a disagreement with the feature per se but that it's not the only editor they use. Since they use multiple editors it's counter-intuitive to switch paradigms related to backspace so they prefer a subset of the vim features.
tl;dr: Add this to your vimrc to make the backspace work like in most other programs:
Though the default behaviour may be surprising, the backspace "not working" can be considered a feature; it can prevents you from accidentally removing indentation, and from removing too much text by restricting it to the current line and/or the start of the insert.
:help 'backspace'tells us:
Influences the working of `<BS>`, `<Del>`, `CTRL-W` and `CTRL-U` in Insert mode. This is a list of items, separated by commas. Each item allows a way to backspace over something: value effect indent allow backspacing over autoindent eol allow backspacing over line breaks (join lines) start allow backspacing over the start of insert; CTRL-W and CTRL-U stop once at the start of insert.
So what do these values mean exactly?
Vim adds automatic indentation for many filetypes; by default, you're not allowed to backspace over this; the rules of what is considered to be 'autoindentation' are somewhat subtle, for example, if we would type this (where █ is the cursor):
if :; then █
Backspacing won't work.
But if we would then add a command and the
fi, and go back up, we are allowed to remove the indentation:
if :; then █: fi
This is because in the first example, Vim determined it should add 1 level of indentation when you pressed Enter; but in the second example, Vim didn't autoindent anything, it's just Tab characters or a few spaces.
This should be the most obvious, pressing Backspace also removes EOL markers (
\r\n); if disabled, Backspace will do nothing if you try to delete a EOL marker.
This means you can only delete text that you've inserted since insert mode started, and you can't delete any text that was previously inserted.
So what's the default setting?
I noticed this using GVIM in windows. The backspace seems to work as expected while using VIM from the terminal in most Linux systems however.
The reason for this is because many Linux distro's ship with pre-made vimrc files which set some common options. For example on my Arch Linux system I have
set nocompatible set backspace=indent,eol,start " ... and a few more...
If you install Vim on Windows, the default is to use the default vimrc and gvimrc that Vim ships with.
The Vim default is an empty value for
backspace. The Arch Linux, Debian, CentOS, or
$other_distrodefault may be different.
This question is about Backspace in insert mode, but let me also add some brief remarks about Backspace in normal mode.
In normal mode, the Backspace acts as
h, it just goes to the left.
By default, the backspace will go to the previous line if at the start of a line (as if
backspace); you can control this behaviour with the
'whichwrap'option through the
bflag (enabled by default).
You can also make backspace delete characters by mapping it to the
nnoremap <BS> X