Vim Tips Wiki
(merge in 1284 + Metacosm's "Fix Backspace")
(Undo revision 38707 by 94.56.101.54 (talk))
Tag: sourceedit
(35 intermediate revisions by 24 users not shown)
Line 2: Line 2:
 
|id=68
 
|id=68
 
|previous=67
 
|previous=67
|next=69
+
|next=71
 
|created=2001
 
|created=2001
 
|complexity=basic
 
|complexity=basic
Line 18: Line 18:
   
 
==Backspace key won't move from current line==
 
==Backspace key won't move from current line==
  +
Backspace works in Insert mode (e.g. not inserting a ^?), but won't delete over line breaks, or automatically-inserted indentation, or the place where insert mode started:
Backspace works, but won't go up or down a line.
 
 
<pre>set backspace=2 " make backspace work like most other apps</pre>
<pre>
 
  +
set backspace=2 " make backspace work normal (non-vi style)
 
  +
Alternately, add this to your .vimrc:
set whichwrap+=<,>,h,l " backspace and cursor keys wrap to next/prev lines
 
  +
<pre>set backspace=indent,eol,start</pre>
</pre>
 
  +
  +
See {{help|'backspace'}}
   
 
==Strange characters are inserted==
 
==Strange characters are inserted==
Line 30: Line 32:
   
 
There's also a bug in xterm (at least v224) that may bring other users into the same backspace problem. Here's the link: http://bugs.gentoo.org/show_bug.cgi?id=154090.
 
There's also a bug in xterm (at least v224) that may bring other users into the same backspace problem. Here's the link: http://bugs.gentoo.org/show_bug.cgi?id=154090.
  +
  +
If backspace doesn't work properly in insert mode, e.g. inserting '^?', try putting this your .bashrc:
  +
<strong>stty erase '^?'</strong>
   
 
==Delete key problems==
 
==Delete key problems==
Line 35: Line 40:
   
 
:If your delete key terminal code is wrong, but the code for backspace is alright, you can put this in your vimrc:
 
:If your delete key terminal code is wrong, but the code for backspace is alright, you can put this in your vimrc:
:<tt>:fixdel</tt>
+
:<pre>:fixdel</pre>
 
:This works no matter what the actual code for backspace is.
 
:This works no matter what the actual code for backspace is.
  +
:: This is, at best, horrifically misleading. In particular, this will break things for Linux users, and for any other Unix system that has "seen the light". For most modern terminal emulators, <BS> sends ^? and &lt;Del> sends ^[[3~ - and putting :fixdel into your .vimrc will change t_kD from the correct ^[[3~ to the incorrect ^H.
   
  +
For many terminal emulators, Backspace will send either <C-?> or <C-h>, and Ctrl-Backspace will emit the other. So, if you don't want to spend the time to fix your settings, you might be able to work around your problems using ctrl+backspace instead of backspace.
I use this on OS X running Vim 7.0 under an xterm on X11. I also find "set bs=2" to be very useful for getting sensible backspace behaviour.
 
   
  +
==Checking for bad mappings==
{{todo}}
 
  +
Your backspace key may be broken due to a bad mapping which has been loaded into Vim. This may be unintentional; Vim sees CTRL-H as a backspace (because CTRL-H is the ASCII code for a backspace), so you also cannot map anything to that. You can check if there are any mappings set, and where they came from, like this:
*Identify what following advice applies to. Presumably only gvim. What systems? Why?
 
 
<pre>
  +
:verbose imap <BS> " show insert-mode mapping for Backspace key
  +
or
  +
:verbose imap ^H " insert this with Ctrl+V then Ctrl+H
 
</pre>
   
  +
If you find a mapping you can try clearing it with:
If your delete key does not work properly, you can instead press Ctrl-Backspace. It works in insert mode and in normal mode.
 
  +
<pre>:iunmap <BS> " remove insert-mode mapping for Backspace key</pre>
  +
  +
If you want to create your own emergency mapping, you could try:
  +
  +
<pre>:imap ^H <Left><Del> " map Ctrl+H to move left and delete the char</pre>
  +
  +
If you want an even more authentic mapping for a malfunctioning Backspace key, try this function:
  +
<pre>
  +
func Backspace()
  +
if col('.') == 1
  +
if line('.') != 1
  +
return "\<ESC>kA\<Del>"
  +
else
  +
return ""
  +
endif
  +
else
  +
return "\<Left>\<Del>"
  +
endif
  +
endfunc
  +
</pre>
  +
  +
And add put this mapping into your .vimrc:
  +
<pre>
  +
inoremap <BS> <c-r>=Backspace()<CR>
  +
</pre>
   
 
==See also==
 
==See also==
Line 49: Line 85:
   
 
==Comments==
 
==Comments==
  +
Might want to add following info somewhere in tip:
  +
  +
On many Linux systems, running in an xterm window:
  +
*Backspace key emits <BS> (8 or ^H)
  +
*Ctrl-Backspace key combination emits &lt;Del> (127 or ^?)
  +
  +
I got it to work on my system by creating a .vimrc file in the home directory with the line:
  +
set t_kb=^?
  +
where for ^? I pressed backspace. Don't forget to open a new terminal window after your changes in .vimrc
  +
  +
On some Linux systems, pressing backspace in xterm or uxterm will move the cursor left (without deleting the character). To fix, add:
  +
xterm.*backarrowKey: false
  +
To your .Xresources file and run:
  +
xrdb -merge ~/.Xresources
  +
----
  +
  +
Alternatively, in .vimrc, type this:
  +
"imap ^? ^H"
  +
  +
----
  +
  +
My terminal sends ^? for backspace and ^[[3~ for delete. To get these keys working in vim like they work in other programs, I put
  +
<pre>
  +
:set backspace=indent,eol,start
  +
:set t_kb=^?
  +
:set t_kD=^[[3~
  +
</pre>
  +
into my .vimrc. (Of course, using ^V <backspace> and ^V <delete> to insert the characters.)

Revision as of 12:29, 6 November 2015

Tip 68 Printable Monobook Previous Next

created 2001 · complexity basic · version 6.0


On some systems, you may experience problems using the backspace or delete keys. This tip discusses the causes and solutions. Generally, these problems arise on Unix-based systems because of the wide variety of hardware and software involved.

 TO DO 

  • Following is a rough merge from some mysterious tips.
  • Need to reword and clarify what each is talking about.

Backspace key won't move from current line

Backspace works in Insert mode (e.g. not inserting a ^?), but won't delete over line breaks, or automatically-inserted indentation, or the place where insert mode started:

set backspace=2 " make backspace work like most other apps

Alternately, add this to your .vimrc:

set backspace=indent,eol,start

See :help 'backspace'

Strange characters are inserted

Backspace just puts weird characters in my file.

See :help :fixdel.

There's also a bug in xterm (at least v224) that may bring other users into the same backspace problem. Here's the link: http://bugs.gentoo.org/show_bug.cgi?id=154090.

If backspace doesn't work properly in insert mode, e.g. inserting '^?', try putting this your .bashrc:

stty erase '^?'

Delete key problems

From :help :fixdel:

If your delete key terminal code is wrong, but the code for backspace is alright, you can put this in your vimrc:
:fixdel
This works no matter what the actual code for backspace is.
This is, at best, horrifically misleading. In particular, this will break things for Linux users, and for any other Unix system that has "seen the light". For most modern terminal emulators, <BS> sends ^? and <Del> sends ^[[3~ - and putting :fixdel into your .vimrc will change t_kD from the correct ^[[3~ to the incorrect ^H.

For many terminal emulators, Backspace will send either <C-?> or <C-h>, and Ctrl-Backspace will emit the other. So, if you don't want to spend the time to fix your settings, you might be able to work around your problems using ctrl+backspace instead of backspace.

Checking for bad mappings

Your backspace key may be broken due to a bad mapping which has been loaded into Vim. This may be unintentional; Vim sees CTRL-H as a backspace (because CTRL-H is the ASCII code for a backspace), so you also cannot map anything to that. You can check if there are any mappings set, and where they came from, like this:

:verbose imap <BS>            " show insert-mode mapping for Backspace key
   or
:verbose imap ^H              " insert this with Ctrl+V then Ctrl+H

If you find a mapping you can try clearing it with:

:iunmap <BS>                  " remove insert-mode mapping for Backspace key

If you want to create your own emergency mapping, you could try:

:imap ^H <Left><Del>          " map Ctrl+H to move left and delete the char

If you want an even more authentic mapping for a malfunctioning Backspace key, try this function:

func Backspace()
  if col('.') == 1
    if line('.')  != 1
      return  "\<ESC>kA\<Del>"
    else
      return ""
    endif
  else
    return "\<Left>\<Del>"
  endif
endfunc

And add put this mapping into your .vimrc:

inoremap <BS> <c-r>=Backspace()<CR>

See also

Comments

Might want to add following info somewhere in tip:

On many Linux systems, running in an xterm window:

  • Backspace key emits <BS> (8 or ^H)
  • Ctrl-Backspace key combination emits <Del> (127 or ^?)

I got it to work on my system by creating a .vimrc file in the home directory with the line: set t_kb=^? where for ^? I pressed backspace. Don't forget to open a new terminal window after your changes in .vimrc

On some Linux systems, pressing backspace in xterm or uxterm will move the cursor left (without deleting the character). To fix, add:

xterm.*backarrowKey: false

To your .Xresources file and run:

xrdb -merge ~/.Xresources

Alternatively, in .vimrc, type this: "imap ^? ^H"


My terminal sends ^? for backspace and ^[[3~ for delete. To get these keys working in vim like they work in other programs, I put

:set backspace=indent,eol,start
:set t_kb=^?
:set t_kD=^[[3~

into my .vimrc. (Of course, using ^V <backspace> and ^V <delete> to insert the characters.)