.vimrc and interrupted ssh can lead to problems, requiring terminal reset

I discovered something neat today. I have the following script in my .vimrc for all of my shells, as well as my local machine:

if &term =~ “xterm.*”
let &t_ti = &t_ti . “e[?2004h”
let &t_te = “e[?2004l” . &t_te
function XTermPasteBegin(ret)
set pastetoggle=[201~
set paste
return a:ret
endfunction
map [200~ XTermPasteBegin(“i”)
imap [200~ XTermPasteBegin(“”)
cmap [200~
cmap [201~
endif

This isn’t really a problem with local instances of vim, but remote instances, if the connection is terminated while the editor is active, it will leave this edit mode enabled in the terminal client on OS X, whether it’s Terminal.app or iTerm2.

This results in all pastes getting surrounded with `[200~` and `[201~`, which only appear in echoes as `0~` and `1~`. This can really be annoying when doing any minor editing, and can make pasting passwords into prompts quite confusing if you don’t know what’s going on.

The fix, quite simply, is to issue a `reset` command to the terminal, which will restore it to normal operation.

Noting this error and fix down for future reference.

Author: Chris

I program software in various computer languages, lately C and C++.

Leave a Reply