7

Sometimes vim will leave something, i.e. press any key to continue, on the terminal and is there any way to return to a clear terminal after exiting vim? I am new to vim and please tell me exactly what I should do.

Sorry I did not express my idea clear enough the first time. What I actually want to ask is that is there a way to return to a clear terminal after typing :q in vim without further input of commands. I am using VIM 7.4 in Ubuntu, terminal type is xterm.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
jcxl
  • 145
  • 1
  • 2
  • 9

6 Answers6

11

Add to your ~/.vimrc:

au VimLeave * :!clear
EntangledLoops
  • 1,951
  • 1
  • 23
  • 36
4

I use an alias to clear on any exit method including :q, :wqa. (this is for my osx brewed binary, find your own vim binary with which vim)

# .zshrc
alias vim="/usr/local/Cellar/vim/8.0.0094/bin/vim && clear"
Plato
  • 10,812
  • 2
  • 41
  • 61
  • 1
    So simple, so powerful, yet so underappreciated :) –  Feb 14 '18 at 18:45
  • 2
    This solution sort of works, but isn't great because it prevents passing arguments to vim and `clear` doesn't actually clear the scrollback buffer unless the E3 capability is defined (and it isn't in bash on standard Ubuntu). – EntangledLoops Jul 13 '18 at 17:57
  • I agree, I found it lacking as well and have not found a solution i'm really happy with – Plato Jul 16 '18 at 02:02
  • 1
    I was 100% inspired by this Answer, but to address the comments on this Answer: Add the following line with reason to your `.bashrc`: `function vim { vim "$@" && clear; }` This lets you pass arguments to `vim` but also clears the terminal screen after `vim` is quit in any way. I apologize if this doesn't apply to `zsh` or `xterm`! – LT 'syreal' Jones Nov 10 '20 at 20:31
2

There is a way to do more stuffs by editing your .vimrc file

Add this to your .vimrc

command Qc :call ClearAndExit()
function ClearAndExit()
    :!clear
    :q!
endfunction

use :Qc to quit.... it will clear the screen as well

gevorg
  • 4,835
  • 4
  • 35
  • 52
manoj
  • 3,391
  • 2
  • 20
  • 30
1

If Vim is compiled with support for switching xterm-screens, it can do this by default, if you set the t_ti and t_te (Vim usually figures out, to what values this needs to be set by itsself). The gory details are explained at :h xterm-screens (pasted below)

(From comp.editors, by Juergen Weigert, in reply to a question)

:> Another question is that after exiting vim, the screen is left as it :> was, i.e. the contents of the file I was viewing (editing) was left on :> the screen. The output from my previous like "ls" were lost, :> ie. no longer in the scrolling buffer. I know that there is a way to :> restore the screen after exiting vim or other vi like editors, :> I just don't know how. Helps are appreciated. Thanks. : :I imagine someone else can answer this. I assume though that vim and vi do :the same thing as each other for a given xterm setup.

They not necessarily do the same thing, as this may be a termcap vs. terminfo problem. You should be aware that there are two databases for describing attributes of a particular type of terminal: termcap and terminfo. This can cause differences when the entries differ AND when of the programs in question one uses terminfo and the other uses termcap (also see +terminfo).

In your particular problem, you are looking for the control sequences ^[[?47h and ^[[?47l. These switch between xterms alternate and main screen buffer. As a quick workaround a command sequence like echo -n "^[[?47h"; vim ... ; echo -n "^[[?47l" may do what you want. (My notation ^[ means the ESC character, further down you'll see that the databases use \E instead).

On startup, vim echoes the value of the termcap variable ti (terminfo: smcup) to the terminal. When exiting, it echoes te (terminfo: rmcup). Thus these two variables are the correct place where the above mentioned control sequences should go.

Compare your xterm termcap entry (found in /etc/termcap) with your xterm terminfo entry (retrieved with "infocmp -C xterm"). Both should contain entries similar to: :te=\E[2J\E[?47l\E8:ti=\E7\E[?47h:

PS: If you find any difference, someone (your sysadmin?) should better check the complete termcap and terminfo database for consistency.

NOTE 1: If you recompile Vim with FEAT_XTERM_SAVE defined in feature.h, the builtin xterm will include the mentioned "te" and "ti" entries.

NOTE 2: If you want to disable the screen switching, and you don't want to change your termcap, you can add these lines to your .vimrc: :set t_ti= t_te=

Christian Brabandt
  • 8,038
  • 1
  • 28
  • 32
0

Yes. You most certainly can.

Use the UNIX command to clear the screen. clear

Tyler Brown
  • 135
  • 2
  • 3
    Or type Control-L at the shell prompt. – Keith Thompson Jul 23 '15 at 18:40
  • An FAQ ([Why doesn't the screen clear when I type control/L?](http://invisible-island.net/xterm/xterm.faq.html#xterm_form_feed)). – Thomas Dickey Jul 24 '15 at 16:32
  • Sorry I did not express my idea clear enough. What I actually want to ask is that is there a way to return to a clear terminal after typing :q in vim without further input of commands. – jcxl Jul 24 '15 at 20:03
0

I didn't want to have to use a different command to exit Vim (e.g. :Qc as suggested) by manoj, and the EntangledLoops' .vimrc method didn't work for me.

Inspired by Plato's answer, I found a similar solution by putting the following function in .bashrc:

# Vim exits to clear terminal screen
function vim {
   /usr/bin/env vim "$@" && clear
}

Using only vim inside the function was problematic due to the function calling itself recursively and creating an infinite loop, but adding /usr/bin/env eliminates this issue, ignoring the function and executing the first vim in the PATH.

jared
  • 141
  • 10