I'm using a script to load files in vim remotely into one gvim instance per desktop viewport. It works fine, except that it causes an empty additional window, that will remain after closing all others, and needs extra keystrokes to quit.

Are there simple means of quitting in that situation, without messing up others like creating a new text initially empty, which creates the same situation of one empty window?

Normally, the situation before the one empty window is having a second tab containing one window.
So that could be made use of, by checking the state after closing a tab.
But that's more of a workaround than a clean solution.

  • 8
    Can you give more information about this: "I'm using a script to load files in vim remotely into one gvim instance per desktop viewport" Commented May 22, 2015 at 12:13
  • @AlexanderMyshov I use the position of the current view port relative to the desktop in the vim instance name; So I can either find the existing instance, or create one for the current view port. Commented Aug 31, 2015 at 5:35

3 Answers 3


The way I understood your question, you have a script which opens several tabs in a vim instance, you do stuff and end with 2 tabs opened in vim, and one of these tabs is empty. When you close the non empty tab, vim should close too since the remaining tab is useless.

In this case you could use the following function + autocommand:

autocmd! WinEnter * call CloseVimOnLastWindow()

function! CloseVimOnLastWindow()
    " If we are on the last tab, if it contains only one window
    " and this window doesn't contains a buffer representing a file
    if (tabpagenr('$') == 1 && winnr() == 1 && len(expand('%'))==0)

With WinEnter you'll trigger the function each time you enter a new window, which includes the situation where you close your tab and enter in the last one which is useless.

When the function is called it checks 3 things:

  • The number of remaining tabpages, verifying if we only have one last remaining tab.

  • The number of windows in this pages: If your last tab contains more than one window you're not in the situation you described so no action is triggered.

  • The length of the buffer name to see if the buffer in this last window doesn't represent an actual file.

If these 3 conditions are filled up, then the function closes Vim.


I found possible solution in this answer at SuperUser "Vim: quit if buffer list is empty":

:autocmd BufDelete * if len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1 | quit | endif

Not sure if I understand the question but if you only want to close all the files then this is the way:


or just:


Your Answer

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

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