vim backspace leaves ^?
Solution 1
^?
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.
Solution 2
Try adding:
noremap! <C-?> <C-h>
to your ~/.vimrc
.
This maps C-? to backspace, and worked for me.
Solution 3
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
Solution 4
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!
Solution 5
On Mac, if you are using Terminal, go to Preferences -> Profiles -> Advanced, then select "Delete Sends Control-H"
Comments
-
Sangeeth Saravanaraj almost 2 years
In Vim, when I hit the
backspace
key in theinsert
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 ofw
to save, I pressbackspace
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 followsspeed 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
-
Sangeeth Saravanaraj about 12 yearsYou are correct. It is the
stty
in my bashrc file which screwed things up. Thanks for clarifying. -
statquant over 10 yearsMe too... using exceed ---> SSH ---> RHEL > ksh > tmux > vim
-
j03m about 10 yearsHappened to a few folks here using putty on unix (not linux) in putty it's an easy fix pref->terminal->keyboard->backspace change to ^H
-
User about 9 yearsWhat does
stty erase ^? 2>/dev/null
do? -
User about 9 yearsI don't have any stty in my vim.
-
horta about 8 yearsI 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.
-
rrlamichhane over 7 yearsI 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 '^?'
-
Michael Mathews over 7 yearsSeems like conflicting advice. Some say add an stty command to your .bashrc and others say to take it out.
-
Steve Bond almost 7 yearshorta's modification worked for me, while ^H did not: imap ^? <BS>
-
Gordonium about 6 yearsevil otto - I too have faced this but I have nothing in my
.bashrc
or similar config files that adjust thestty
settings. Instead, I can reproduce this on macOS usingfind . -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 typereset<CR>
. If I usevim $(find ...)
instead then the backspace key and shell work correctly. Any ideas why? -
Aaron Robeson almost 6 yearsTook me forever trying out other solutions but this did it for me on a mac, cheers!
-
Avrdan about 5 yearsThis 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)
-
artemis about 4 yearsI've spent countless hours trying to fix this, and this did it for me, thank you
-
wisbucky over 2 yearsWorked for me. So I guess Mac Terminal sends
^?
by default? -
Choylton B. Higginbottom over 2 years@wisbucky There's a difference in how difference systems send backspace superuser.com/a/129986/430236