vim backspace leaves ^?
Asked Answered
M

16

76

In Vim, when I hit the backspace key in the insert mode, it leaves ^? character and does not delete the character it is suppose to delete.

I have the following in my .vimrc

syntax on
set number
set expandtab
set incsearch
set nocompatible
set backspace=indent,eol,start
fixdel

This happens in the command mode too. When I wrongly type W instead of w to save, I press backspace key and it gives me the following:

:W^?

Any idea on whats wrong and how to fix it?!

UPDATE: before posting this question to SO, I have done a basic google search and tried all the suggestion from the first page of search result but unsuccessful.

@strcat I'm using vim version 7.0.237, KDE console 1.6.4, Linux 2.6.18 x86_64 machine.

@graywh w.r.t cat -v, for delete, I get ^[[3~ and for backspace, I get ^?.

The output of stty -a is as follows

speed 38400 baud; rows 38; columns 194; line = 0;
intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O;
min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -ixon -ixoff -iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
Milamilady answered 14/3, 2012 at 11:48 Comment(9)
Please check the link bellow to help debug your problem: > vim.wikia.com/wiki/Backspace_and_delete_problems EDIT: i found what it seems like a similar problem with the one described by you: > lj4newbies.blogspot.com/2007/05/…Checkroom
Sorry I tried that but didn't work!Milamilady
i've updated my post with some new hints, please revise themCheckroom
which terminal/OS do you use?Sanchez
In cat -v, what is printed when you hit Delete and Backspaces keys? What is your output from stty -a?Columella
Totally off-topic here, might be a good question for superuser or unix.stackexchangeUsed
@Sanchez I'm using vim version 7.0.237, KDE console 1.6.4, Linux 2.6.18 x86_64 machine.Milamilady
For me, this happened while changing the qTerminal settings by changing the terminal emulation from default to linux.Zeculon
I had this problem but on MacOS using iterm. I am trying to fix it on a remote solaris host connected via ssh, but in attempting to fix it, the issue is now locally on iterm.Nitrochloroform
M
92

^? is the delete character; the backspace character is ^H. Only one of these is recognized by your terminal as "erase", and this is determined by the terminal settings, stty. (bash and other shells understand this as a problem and do special things to recognize both)

If your terminal emulator (ssh, putty, xterm, whatever) disagrees with your terminal settings, then you see this behavior. Usually it's right by default, but very often people will put stty commands in their .bashrc which breaks things.

You probably have something like stty erase ^H in your bashrc. If you do, get rid of it, or change your terminal settings to have backspace send ^H instead of DEL (^?)

You can also fix this with vim mappings, but that's ignoring the basic problem.

Martie answered 17/4, 2012 at 19:40 Comment(7)
You are correct. It is the stty in my bashrc file which screwed things up. Thanks for clarifying.Milamilady
Happened to a few folks here using putty on unix (not linux) in putty it's an easy fix pref->terminal->keyboard->backspace change to ^HHuckster
What does stty erase ^? 2>/dev/null do?Osmund
I don't have any stty in my vim.Osmund
I want to add that if you don't have 'stty' on your .bashrc file and you're seeing this issue... then add 'stty' to your .bashrc file and this issue will go away. stty erase '^?'Degust
Seems like conflicting advice. Some say add an stty command to your .bashrc and others say to take it out.Piranesi
evil otto - I too have faced this but I have nothing in my .bashrc or similar config files that adjust the stty settings. Instead, I can reproduce this on macOS using find . -type f -name "foo" | xargs vim and then trying to search for something in Vim using /. At this point, hitting backspace enters ^?. But, if I hit backspace while holding the Fn key, it works correctly. Also, after exiting Vim the shell doesn't let me see any characters I enter so I have to blindly type reset<CR>. If I use vim $(find ...) instead then the backspace key and shell work correctly. Any ideas why?Nasa
C
18

Try adding:

noremap! <C-?> <C-h>

to your ~/.vimrc.

This maps C-? to backspace, and worked for me.

Circuity answered 7/11, 2017 at 7:49 Comment(1)
In general, anytime this remap fixes the issue, it means that your termios settings don't match the terminal and fixing those (with the stty command) will fix not only Vim, but many other terminal applications together so, instead, I generally recommend adding this to your ~/.profile (or equivalent: stty erase '^?'Smaltite
C
9

From the vim wiki Backspace_and_delete_problems, I went on to read :help :fixdel it suggests this:

if &term == "termname"
  set t_kb=^V<BS>
  fixdel
endif

Where "^V" is CTRL-V and "" is the backspace key
(don't type four characters!). Replace "termname"
with your terminal name.

For me the fixdel makes the backspace work like delete. My first mistake was also doing the CTRL-V backspace in gvim, do on the system that you can not get the key to work properly so it pastes the backspace key that the problematic session sees.

I now have in my .vimrc:

if &term == "xterm-256color"
  set t_kb=^?
endif
Cyclamen answered 11/4, 2014 at 11:38 Comment(0)
R
6

On Mac, if you are using Terminal, go to Preferences -> Profiles -> Advanced, then select "Delete Sends Control-H"

Reede answered 3/5, 2016 at 18:58 Comment(3)
Took me forever trying out other solutions but this did it for me on a mac, cheers!Nimrod
Worked for me. So I guess Mac Terminal sends ^? by default?Valedictory
@Valedictory There's a difference in how difference systems send backspace superuser.com/a/129986/430236Reede
E
5

A good fix for this problem is to set the "Terminal > Keyboard" settings to map the Backspace key to "Control-H" in PuTTY; This is if you are using PuTTY and experiencing the "^?" problem when pressing the Backspace key.

I've created an article on this here:

https://alvinbunk.wordpress.com/2017/08/23/vi-or-vim-using-backspace-inserts/

Thanks to @j03m for the suggestion!

Exhibitionist answered 23/8, 2017 at 16:44 Comment(1)
I've spent countless hours trying to fix this, and this did it for me, thank youPraefect
P
4

Try ctrl+del/ctrl+backspace or alt+del/alt+backspace, I cant remember which, but I think it will do the trick for you.

If that doesn't work try shift+backspace/del. I've ran into this problem before, there is a combo key that you can press to send the correct char code to do your deleting.

Piston answered 18/4, 2012 at 2:17 Comment(0)
P
4

I had the same issue where vim backspace leaves ^?, while in command line it worked properly.

This is what worked for me, using vim mapping:

In .vimrc, type:      imap ^? ^H

This will map CTRL-H (which is the default backspace in vim) to the backspace key. Hope that helps.

Peplos answered 12/4, 2014 at 0:26 Comment(2)
I couldn't figure out how to get the ^H character to work so instead I did: imap ^? <BS> in the .vimrc file and it works fine now. cmap is also necessary for command and searching.Phytology
horta's modification worked for me, while ^H did not: imap ^? <BS>Abound
J
3

Had exactly same problem, except that Ctrl+H wasn't working for me. stty settings are also fine. Just wanna mention my environment, if somebody will got same problems:

  • Using cygwin with xterm, under X ofcourse :)
  • Ssh'ed to RHEL6.4->bash->tmux->vim

After adding fixdel to .vimrc it works! All fixes above wasn't working for me.

Jugal answered 2/8, 2013 at 9:23 Comment(1)
Me too... using exceed ---> SSH ---> RHEL > ksh > tmux > vimUnconquerable
I
3

put stty erase ^? in your .bashrc

Run your .bashrc

run command: stty -a

speed 38400 baud; rows 48; columns 157; line = 0;
intr = ^C; quit = ^\; ***erase = ^?***; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

Erase will be changed by now with new value ^? and your backspace shall work now.

Idyll answered 28/11, 2013 at 11:55 Comment(0)
M
3

For me, it was a stty problem, I had a similar stty -a output as yours.
It got fixed for me by running stty sane.

Not only did this fix the backspace/del problem, but also fixed the arrow keys.

Musty answered 15/5, 2019 at 15:57 Comment(0)
B
2

This answer worked for me: http://vim.wikia.com/wiki/Backspace_and_delete_problems

Basically, putting set backspace=2 in .vimrc works.

Brunhilde answered 7/8, 2018 at 9:38 Comment(1)
This worked for me as well. Simple and to the point. The only thing is, I put my changes in the global vim configuration under /etc/vimrc (edited with sudo)Dovetail
E
0

If you are using KDE, I know that pain. This is the most comprehensive solution I have found so far http://www.yalla.nu/blog/post/86

Ezraezri answered 30/5, 2014 at 2:47 Comment(0)
T
0

I had this problem and found the following setting in my .vimrc:

:set t_kb=^H

Removing that line solved the problem.

Tape answered 14/3, 2016 at 14:0 Comment(0)
K
0

Under FreeBSD with CSH you should replace this in your .cshrc:

bindkey "^W" backward-delete-word

With this :

bindkey "^?" backward-delete-word
Kermie answered 10/7, 2018 at 14:45 Comment(0)
T
0

For mac OS Big Sur fn+backspace worked for me

Tiger answered 26/10, 2021 at 13:43 Comment(0)
T
0

I was having a similar problem while running vi in putty terminal. I fixed it by changing the settings of the putty.

change settings > Terminal > Keyboard > The Backspace Key (select Control-H)
Tepper answered 11/5, 2022 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.