Vim, Terminal.app, and the Delete key

by
Annika Backstrom
in misc, on 9 July 2008. It is tagged #Personal, #Bash, #delete, #Mac OS X, #screen, #term, #termcap, and #terminal.app.

One of my SSH servers continually gives me problems with the Mac "Delete" key in Vim. While the key always works correctly in bash, it will only work in Vim if I am inside a screen session. If I have not opened screen, Delete will function as "Forward Delete." Oddly, the key is sending ^H in both cases. This turned out to be a termcode issue.

Fixing by Setting \$TERM

I failed to fix this with ~/.inputrc, but did have success after tweaking $TERM. Initially my $TERM=xterm-color, which fails. When $TERM=screen, Delete works. Setting $TERM=xterm works, as well, which means the following in ~/.bashrc can be used as a fix:

if [ "$TERM" = "xterm-color" ]; then
    export TERM=xterm
fi

Fixing by Remapping Delete

A second solution is to change Vim's terminal options. :set termcap will show you what t_kD (<Del>) is currently set to. When I'm using TERM=xterm-color, that value is ^H. Remapping t_kD in .vimrc makes Delete consistent across $TERM settings:

set t_kD=<Ctrl-v><fn-Delete>

Use the modifier keys Ctrl-v and fn-Delete, which will expand to set t_kD=^[[3~. Best practices would probably explicitly set t_kb (backspace) as well.