How to Stop Ubuntu from Automatically Clearing the Terminal Screen on Exit

If you use a command-line text editor like nano, vim, or less in the Ubuntu terminal, you might have noticed a jarring behavior when you finish your work. When you type exit or press q to close the program, the terminal screen suddenly violently clears itself. All the text you were just reading vanishes, and you are dumped back to a clean, empty command prompt.

This happens because modern terminal emulators use a feature called the “Alternate Screen” (altscreen). When a program launches, it switches to a secondary, temporary display buffer. When the program closes, it switches back to the primary buffer, effectively erasing everything that happened in the secondary buffer. If you were using less to read a massive log file and you quit the program because you finally found the error code you needed, that error code is immediately wiped from the screen, forcing you to memorize it.

You can explicitly disable the Alternate Screen feature, forcing programs to print their output directly into your primary terminal history, ensuring the text remains visible on the screen long after the program has closed.

How to Disable the Alternate Screen (Altscreen)

You must configure your terminal’s terminfo database to ignore the smcup and rmcup commands.

  1. Open your terminal application (Ctrl+Alt+T).
  2. You need to create a custom terminal configuration file. Run the following command:
    infocmp > ~/terminfo.src
  3. Open this new file in a text editor:
    nano ~/terminfo.src
  4. Look closely at the massive block of comma-separated variables. You are looking for two specific entries:
    smcup=\E[?1049h (or similar)
    rmcup=\E[?1049l (or similar)
  5. Carefully delete those two specific variables (smcup and rmcup) and their associated escape codes from the file. Leave the rest of the file untouched.
  6. Press Ctrl+O then Enter to save the file, and Ctrl+X to exit nano.
  7. Compile your new configuration by running the tic command:
    tic ~/terminfo.src

Restart your terminal. The next time you open nano or less and then quit, the text editor’s interface and all the text you were reading will simply remain printed on the screen right above your new command prompt, allowing you to easily copy, paste, and reference the data.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.