241

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?
6
  • 3
    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
    Commented Feb 24, 2015 at 13:04
  • 8
    @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. Commented Feb 24, 2015 at 15:24
  • 5
    @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
    Commented Feb 24, 2015 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
    Commented Jun 17, 2018 at 8:01
  • 1
    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
    Commented Jul 23, 2019 at 7:46

6 Answers 6

298

tl;dr: Add this to your vimrc to make the backspace work like in most other programs:

In vimscript:

set backspace=indent,eol,start

In lua:

vim.opt.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
6
  • 1
    I think that "_X would be "more natural" behaviour for <BS>.
    – Hauleth
    Commented Jan 1, 2018 at 16:29
  • 3
    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
    Commented Jun 17, 2018 at 8:02
  • 1
    @krehwell, about your edit, vimrc is not always .vimrc, since on Windows platforms it's actually called _vimrc. Recent versions of Vim also accept ~/.vim/vimrc or vimfiles\vimrc on Windows. So I actually prefer Martin's original generic phrasing here.
    – filbranden
    Commented Aug 8, 2020 at 18:58
  • I linked to it :help vimrc now, although that's perhaps not the gentlest of introductions as it contains quite a few things that don't really matter. Commented Aug 10, 2020 at 6:47
  • How to do this for nvi? It does not support set backspace.
    – t7e
    Commented Jul 30, 2022 at 13:49
6

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

4
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
2
  • 6
    That seems pretty weird to me, could you explain why adding another command to your vimrc would make the previous one work?
    – statox
    Commented Aug 30, 2017 at 16:20
  • I placed it at the last line of my .vimrc, and it works perfectly fine! ✌️ Commented Nov 8, 2019 at 9:20
0

None of the solutions worked for me. The problem was that I was using MobaXTerm, which replaced backspace with C-h by default, and had this line in ~/.vimrc:

inoremap <C-h> <Left>

as a result, effectively, all my backspaces were replaced with <Left> keypresses. The solution was to go to MobaXTerm settings -> Terminal -> Local shell settings, and disable "Backspace sends ^H".

1
0

In zsh, you might additionally need to put

bindkey -v '^?' backward-delete-char

to your .zshrc file. Relying on the following response.

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