18

I'm trying to get into Vim. I'm running it in the terminal on OS X.

Anytime I hit the delete key, it simply changes case of that letter instead of deleting it. When I SSH into my server and use Vim there, it deletes normally.

Any ideas what may be going wrong?

1
  • Are you talking about Delete key (Backspace on a PC keyboard)? or Fn-Delete (Delete on a PC)?
    – Dummy00001
    Jul 12, 2010 at 22:06

6 Answers 6

21

The problem

The Del key generates the code ^[[3~ in my urxvt terminal on GNU/Linux, and might generate a similar code in your OS X terminal.

My theory is that Vim for some reason doesn't recognize any keybinding for the delete key, and simply tries to interpret the string ^[[3~ as input instead. ^[ is the keycode for the Esc key (which puts you in normal mode), and ~ is the Vim command for changing the case of a letter (from normal mode).

You can confirm the keycodes I mentioned by pressing Ctrl+V Esc and Ctrl+V Del from insert mode in Vim. Ctrl+V means that the next character should be inserted as text instead of being interpreted by the editor.

The solution

As for the solution, try editing your Vim configuration file (presumably ~/.vimrc):

vim ~/.vimrc

And append the following code to it:

nmap <Ctrl-V><Del> x
imap <Ctrl-V><Del> <Ctrl-V><Esc>lxi

I hope this helps :)

4
  • Off-topic: How do you add graphical keyboard-buttons to StackOverflow posts?
    – jabirali
    Jul 13, 2010 at 0:38
  • 4
    @A Guy With Really Long Nick: use the <kbd>Del</kbd>.
    – Dummy00001
    Jul 13, 2010 at 12:43
  • In my case, I got funny results with the given imap, which I fixed by changing lxi to just xi. For what it's worth, I'm using st on dwm.
    – ter0
    Sep 16, 2014 at 7:20
  • 1
    This works for me for vim on an embedded linux device over ssh. It sould be noted that in the above nmap and imap commands you must not type the strings <Ctrl-V><Del> etc. into .vimrc but press the keys to generate the "special keycodes" like "^[[3~" in the .vimrc file.
    – bns
    Apr 27, 2017 at 8:32
5

The problem was that in my .vimrc I had

set term = ansi

Took that out and all was well - sorry about the troubles, thanks!

1
  • Wow. That was exactly my problem. Now I want to understand why that setting is a problem.
    – Ken Ingram
    Jan 12, 2022 at 2:14
2

Well, this took forever for me to resolve. When using vim I was in Iterm2 on macOSx to access a Centos5 system via gnu screen. Not only was the delete key changing the letter case, and causing delays in vim, but also the arrow keys didnt work. I think the problem was simply in the old configuration on the Centos5 machine, but may have had something to also do with either iterm or gnu screen, but i hadnt had this issue at all on any Centos6 systems.

What did Not work: I compile vim 7.4, that did not resolve it, so don't go down that path. I also tried these configurations that did not work

"set term=ansi
"set backspace=indent,eol,start
"set nocompatible
"fixdel

":if &term == "xterm"
":if &term == "xterm-256"
":  set t_kD=^V<Delete>
":endif

"nmap <Ctrl-V><Del> x
"imap <Ctrl-V><Del> <Ctrl-V><Esc>xi

I finally found the solution.

Solution: Set the following in .vimrc

set term-builtin_xterm

Additionally, you may also notice that 256 colors have an issue on this same system. So if it helps, you can check your TERM environment variable echo $TERM. If it is xterm you can switch it to 256 colors, e.g. export TERM='xterm-256color to get the color schemes back on track for this same system. reference

0

I use vim regularly on my OSX machine (vim version 7.2.108), and I do not have this issue. Try renaming your vimrc file and then reload vim, and see if the issue persists. If there is no issue after renaming you vimrc file, then your issue is in that file.

On my machine, my vimrc file is pretty much empty:

set ruler
set tabstop=2
set cindent
set number
syntax on
0

None of these solutions worked for me, until I found a solution on the vim fandom website.

Those solutions are for a similar problems with arrow keys, which I was experiencing in addition to the delete key problem.

Solution 8 fixed it for me, the problem was that I had remapped <Esc> to <Esc>l in order to move the cursor right every time I entered normal mode, evidently this caused any other key code with included ^[ (which is the escape code) including the arrow keys ( ^[OA ^[OB etc ) and the delete key ( ^[[3~ ) to be misunderstood.

Removing the offending line from my .vimrc fixed the problem.

0

I had the same issue where vim suddenly interpreted keys differently. The answer for me was that the environment variable $TERM had somehow been to to 'dumb' in my terminal.

I added

export TERM=xterm-color

to ~/.bashrc (in my case) and that fixed all the issues with keys; DEL worked as expected etc.

I didn't need to add or change anything in ~/.vimrc

I should mention this affected more than just vim for me - if you also see 'less' behave differently for example, you may have the same cause as I did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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