I have set Vim's default colorscheme to Monokai, and when I exit Vim the Monokai colors are still on the screen until I clear it.

How can Vim be set to run clear automatically on exit, or change the color scheme or at least the background color to what it was before it started?

  • Does that happen with other colourschemes as well? What is TERM set to (echo $TERM in the shell, and :echo $TERM in Vim)
    – muru
    Mar 29 '17 at 8:30
  • @muru They are both xterm-256color. Of course when I type clear after I leave viim, the terminal's normal color returns. What I am looking for would be for Vim to change the background color back to the terminal's background color before it exists. so I will know I am back to the terminal instead of still in Vim screen at a glance. The text colors are not important, just the background.
    – vfclists
    Mar 29 '17 at 8:58

How to Clear the Screen When Exiting Vim

When Vim quits, it sends the escape sequence defined by the t_te setting to the terminal in order to tell it what to do. This should be set automatically by Vim to do something sensible, but it looks like something's going wrong in your setup: the likely candidate seems to be that your terminal is configured incorrectly, but let's investigate:

To clear the screen, we need to send the clear escape sequence to our terminal. We can find out what sequence is required by querying our terminfo database with the infocmp command. By running infocmp in my terminal (which happens, like yours, to be xterm-256color), I see the following entry:


In this output, the left hand side of the equation refers to a terminal "capability", and the right-hand side to the escape sequence used to access it. In the displayed sequence, the \E refers to an Escape character.

So, the escape sequence used by xterm to clear the screen is <Esc>[H<Esc>[2J. If we want Vim to clear the screen on exit, we need to configure its t_te setting to send this sequence.

We do so with the following Vim command:

:set t_te=^[[H^[2J

N.B. In the above, the two instances of ^[ are Vim's representation of the escape character that was displayed as \E above. You type them by pressing Ctrl+VEsc: not by typing a ^ followed by a [ character.

After quitting Vim, the screen should now be cleared.

What Might Be Going Wrong For You

This may or may not actually work for you, however. As already mentioned, Vim should set up t_te sensibly already. There are several places where the problem could be occurring:

  1. Your t_te might be set incorrectly by your .vimrc or by a plugin. Setting t_te as above should fix this,
  2. You have indicated your $TERM is set to xterm-256color. If you are actually using a different terminal, the escape sequence described above may be wrong. The way to fix this is by setting $TERM to match your actual terminal.
  3. It's possible (although less likely) that your terminfo database contains the incorrect info for xterm-256color. You can test if this is the case by running the command tput clear in your terminal. This queries the terminfo database for the clear capability, which is output to the terminal, and should result in the terminal clearing the screen. If this does not do so, then your terminfo database is incorrect, and will need fixing. This is not really a Vim issue, so you may find more help elsewhere.

Extra Credit

I wrote earlier that Vim should set t_te to "something sensible". But for me, this is not clearing the screen. So what does it actually set t_te to?

The command :set t_te?, for me, outputs: t_te=^[[?1049l. By looking in the infocmp output, I can see that this corresponds to a capability of rmcup.

It turns out that this, in conjunction with the related t_ti setting (which is set to the smcup capability) sets up Vim to use xterm's alternate screen buffer for rendering — when Vim quits, the terminal state is reset to display whatever was displaying before I ran Vim.

Again, you can try out this switching of screen buffers outside of Vim by running a sequence of commands in your terminal:

ls         # just to get something onscreen
tput smcup # The ls output disappears
tput rmcup # The ls output reappears


$ date
$ vim --clean # inside just exit :q

If after exit Vim, the terminal still shows all cmds and its outputs, like

$  date
So 18. Jul 14:35:01 CEST 2021
$  vim --clean

... then all is smooth, and you can apply the excellent answer of Rich.

Otherwise check in detail your .vimrc or its plugins. Comment almost everything of .vimrc (except NERDCommenter plugin for easy comment-uncomment), and as soon as it works as expected ($ date; $ vim ~/.vimrc # edit, source and save-exit) uncomment little by little till find the trouble-maker.

I had same problem, it was just hidden redraw (:h redraw).

PD: try with other terminal console, example gnome-terminal or KDE-konsole. If it works in different terminal, then .vimrc is NOT guilty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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