143

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.

  1. Why does this happen?
  2. How can I make the backspace key delete characters as usual?
  3. 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 Feb 24 '15 at 13:04
  • 5
    @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. – Karl Yngve Lervåg Feb 24 '15 at 15:24
  • 3
    @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. – Shahbaz Feb 24 '15 at 15:55
  • 1
    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. – j riv Jun 17 '18 at 8:01
  • If you have installed vim-faq, you can get an offline answer there: :h vim-faq and search /backspace. The hard to memorize tag is :h faq-12.26. – Hotschke Jul 23 at 7:46
173

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 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?

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 :; 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.

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
  • 1
    I think that "_X would be "more natural" behaviour for <BS>. – Hauleth Jan 1 '18 at 16:29
  • 2
    Worth noting that almost no distribution ships with the default behavior. The only one I've noticed was msys2 and that wasn't probably a decision but its nature of being underdeveloped. – j riv Jun 17 '18 at 8:02
5

Just in case anyone is experience not explainable <BS> behavior using ConEmu on Windows, you may have to remap the <BS> key to:

inoremap <Char-0x07F> <BS>
nnoremap <Char-0x07F> <BS>

More information

1
set backspace=2

is used with v5.4 and earlier. worked for me on mac.

looks like on mac i have v3.2:

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

All versions past 3.2 use the GNU General Public License v3 (GPLv3), which Apple isn't in favor of supporting. A discussion on reddit about this is here.

You can upgrade to the newest Bash with:

brew install bash

this allows to take advantage of programmable completion highlighted here.

0

If set backspace=indent,eol,start is the last line of your vimrc, none of solutions will work.

Add one more rule after this, eg:

set ruler
  • 4
    That seems pretty weird to me, could you explain why adding another command to your vimrc would make the previous one work? – statox Aug 30 '17 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.