4

After closing an app that uses color formatting (e.g. vim) the terminal retains some properties like background color. This happens only when using putty-256color or screen term. I'm observing similar behavior in RHEL 6.5 and Ubuntu 14.04LTS.

The only solution is to reset the terminal.

When using xterm-256color term (also w/ Putty terminal emulator) the problem isn't present.

Is there a solution/explanation why this happens and what could I be loosing when using xterm under Putty terminal emulator, i.e. would it be preferable to actually use putty-256color or xterm-256color term?

vobelic
  • 51
  • 1
  • 5
  • 1
    The problem as you are seeing is that the TERM variable is set incorrectly. vim uses this variable to determine how it will draw to the screen. If it is set incorrectly vim is unable to undo the settings it used to draw with because the underlying terminal doesn't understand the codes it uses. – FDinoff Jul 07 '14 at 21:13
  • Not sure what you mean by set incorrectly. I checked the `$TERM` value in all situations and it's like it's supposed to be. – vobelic Jul 08 '14 at 08:14
  • I believe this is due to some redrawing that happens in regular non-screen terminal - after an app is closed the whole "frame" is refreshed and there are no traces of the previous (CLI) app and you are returned to the prompt right after the line where executed the command that launched this app was executed. In screen there's no refresh - when you close an app you are dropped to the prompt right after the app which is still visible in the buffer on a previous line. Is there a way to cause frame/screen refresh in screen term? – vobelic Jul 08 '14 at 08:20

3 Answers3

2

The problem description mentions "the terminal retains some properties like background color". That could refer to the back color erase feature, which PuTTY supports. When the screen is erased, the terminal will fill the background (on the erased portion) with the current background color.

On first glance, the alternate screen feature does not appear to be related, however. I can produce a problem using PuTTY and vim using the "morning" colorscheme with or without PuTTY's alternate-screen feature disabled. On exit, the screen has the same gray background as in vim. If I follow that by

tput sgr0

then that command resets the colors, so that new text is written with the terminal's default background color (as expected).

Looking at the escape sequences sent by vim on exit shows nothing unusual — in the terminal description (using unmap to make them in readable form):

\n
\E[1m
\E[38;5;130mendif
\E[0m
\E[30m  
\E[47m  
\E[24;63H1,1
\E[11CTop   
\E[1;1H  
\E[?25h  
\E[?25l
\E[24;63H
\E[K
\E[24;1H:
\E[?25hq 
\E[?25l 
\E[?25h\r
\E[?25l  
\E[24;1H
\E[K
\E[24;1H
\E[?1l   
\E>
\E[?25h
\E[2J   
\E[?47l

That is, vim sends sgr0 (\E[0m) just after setting the background to gray (\E[38;5;130m). Doing that should reset the colors. But it does not. There are several other operations before vim sends the two-part escape sequence in rmcup

\E[2J
\E[?47l

which should clear the (alternate) screen and switch back to the normal screen. The corresponding capability in xterm is

\E[?1049l

which combines the two operations. Seeing that, there are two problems in PuTTY which together produce the problem:

  • the colors are not reset as expected, and
  • the color is applied to setting the background on the normal screen.

It just happens to work with TERM=xterm, using the 1049 code, because PuTTY's developers apparently tested that. For instance, if that restores the normal screen's colors (but not using the 47 code), then you would see exactly this problem.

Further reading:

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
0

A solution to this is to enable altscreen under GNU screen. Thus the screen term behaves like regular xterm does - a fullscreen app has a separate frame/screen from the rest of the output.

It's important, however to have a correct TERM set so that the app knows how to switch between the regular and alt-screen.

Source: When using vim or less in gnu screen, quitting vim or less leaves a lingering imprint

Community
  • 1
  • 1
vobelic
  • 51
  • 1
  • 5
  • seems this still doesn't fix things for RHEL 6.5. Even though vim correctly switches between regular and altscreen (guess that means TERM is correct) the formatting from vim still remains after vim is closed. With same configuration this works perfectly on Ubuntu 14.04LTS. – vobelic Jul 08 '14 at 11:15
  • One more observation - this happens when exiting vim with `:q`. It DOESN'T happen when closing with `:wq`. – vobelic Jul 08 '14 at 11:23
0

See this response for some inspiration on how to fix this problem:

Main point is to include

altscreen on

in your .screenrc file.

https://stackoverflow.com/a/37863269/5153834

Community
  • 1
  • 1
bloodrootfc
  • 1,133
  • 12
  • 10