tl;dr: Add this to your vimrc
to make the backspace work like in most other programs:
set backspace=indent,eol,start
Longer answer
Though the default behaviour may be surprising, the backspace "not working" can be considered a feature; it can prevent 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?
indent
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 (true) {
█
Backspacing won't work.
But if we would then add a command and the }
, and go back up, we are allowed to remove the indentation:
if (true) {
█fun()
}
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.
Also see :help 'autoindent'
eol
This should be the most obvious, pressing Backspace also removes EOL markers (\n
or \r\n
); if disabled, Backspace will do nothing if you try to delete a EOL marker.
start
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 /usr/share/vim/vimfiles/archlinux.vim
with:
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_distro
default may be different.
Normal mode
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 eol
was in backspace
); you can control this behaviour with the 'whichwrap'
option through the b
flag (enabled by default).
You can also make backspace delete characters by mapping it to the X
command:
nnoremap <BS> X
X
(and delete withx
), but often what you really wanted to do wasdb
for example to delete until the beginning of the word, ordaw
to delete the whole word etc. If you have backspace available, you risk using this suboptimal key in more cases than necessary.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.:h vim-faq
and search/backspace
. The hard to memorize tag is:h faq-12.26
.