Actually that's vim, not vi (the last line on the screen demonstrates that).
Considering that most of the unwanted text would be left in the alternate screen, it seems that you're using a terminal description which supplies empty strings for the smcup
and rmcup
for switching to/from the alternate screen.
Part of that features clears the screen when switching to the alternate screen, and vim's not clearing the screen on its own (unless you press controlL
, for instance, to repaint the screen).
Since people frequently advise editing the terminal description to remove those capabilities, vim's likely to deal with that "as expected", so I'd expect something unusual such as empty strings, e.g.,
smcup=,rmcup=,
in the output of infocmp
. It's unnecessary (and has been for twenty years) with xterm
. See the xterm FAQ Why doesn't the screen clear when running vi?
In the revised question, the TERM
uses xterm-color
which (see the FAQ What $TERM should I use?) is not really recommended (xterm-xfree86
or xterm-new
are preferred). But vim should cope with that (it works on my machine, going from xterm #326 on OSX to Debian 7). However, in a quick check using vim to edit /bin/sh for example, and exiting, it shows some (lesser) confusion about how to clear the screen, leaving some debris.
The distinction between xterm-color and xterm-new is that in the latter, vim (should) see that it is able to clear the screen more readily. In the former, it has to overwrite areas on the screen, and if it became confused (such as with a difference in locale vs the terminal settings) about what shows on the screen, then it will leave noticeable debris.
You can see if xterm
is using UTF-8 encoding by the presence of a check-mark on the (control-right mouse menu) UTF-8 Encoding menu entry. If that is not set, and the locale on the remote server uses UTF-8, then you can get interesting (and surprising) behavior. Just type
locale
on the command-line to see (conventionally the locale variables corresponding to UTF-8 encoding are named with that as a suffix in some way).