8

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.

3
  • vim normally shouldn't do that -- but even if it does, where's the harm? Commented Jul 23, 2015 at 18:41
  • Please specify what platform and what terminal you're using. Commented Jul 23, 2015 at 22:39
  • VIM 7.4 on Ubuntu, terminal type is xterm Commented Jul 24, 2015 at 20:00

6 Answers 6

13

Add to your ~/.vimrc:

au VimLeave * :!clear
Sign up to request clarification or add additional context in comments.

Comments

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"

4 Comments

So simple, so powerful, yet so underappreciated :)
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).
I agree, I found it lacking as well and have not found a solution i'm really happy with
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!
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

4 Comments

before that just export TERM=xterm on your terminal and try the samething
this is the link i got info. from unix.stackexchange.com/questions/60499/…
I have edited the answer with .vimrc changes as well
which one did you try? .vimrc or changing the TERM ?
2

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=

Comments

0

Yes. You most certainly can.

Use the UNIX command to clear the screen. clear

3 Comments

Or type Control-L at the shell prompt.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.