如何在离开文件时自动关闭快速修复窗口?

14
我在 Vim 中使用插件来编译 Tex 文件。当编译过程中出现错误时,这些错误会显示在屏幕底部的快速修复窗口中。
当我想离开正在处理的文件(如:`:q`、`:wq` 等)时,快速修复窗口会很烦人,因为它会在我离开文件缓冲区后仍然保持打开状态,强制我需要在快速修复窗口上也输入 `:q`。
有没有办法告诉 Vim 在使用 `:q` 命令时立即执行 `:close`?我已经尝试了几个 autocmds,但都没有成功。

1
:ccl 有用吗? - Peter Rincker
:ccl:cclose完全相同... - romeovs
4
@Tomalak:不是这样的:http://meta.stackexchange.com/questions/25925/vim-questions-so-or-su - ib.
好吧,我不同意那篇帖子以及它链接到的大多数帖子。关于 PHP 的问题类似于在一个关于驾驶的网站上询问路标。另一方面,在同一网站上你不会问如何更换汽车头灯,就像你不会在这里问如何更换键盘电池 - 这是你编程时需要做的事情。IDE 问题则完全不同,而且 vim 并不仅限于编程世界。 - Lightness Races in Orbit
3个回答

23

将以下内容添加到您的.vimrc文件中

aug QFClose
  au!
  au WinEnter * if winnr('$') == 1 && &buftype == "quickfix"|q|endif
aug END

警告:如果快速修复窗口是唯一可见的窗口(且仅为标签),则这将关闭vim。


你让我开心了。 - ospider
1
注意:对于使用此脚本的任何人,它只有在最后一个打开的窗口是quickfix时才会关闭。如果您打开两个带有相对quickfix窗口的不同文件,并关闭其中一个文件,则其对应的quickfix窗口将保留在屏幕上。 - Dez

3
命令:qa将退出所有打开的窗口。

1

如果要在离开文件时(例如:q:wq等),自动关闭多个快速修复/位置/帮助窗口,请将以下代码添加到您的.vimrc文件中:

" s:NextNormalWindow() {{{2
function! s:NextNormalWindow() abort
    for i in range(1, winnr('$'))
        let buf = winbufnr(i)

        " skip unlisted buffers
        if !buflisted(buf)
            continue
        endif

        " skip temporary buffers with buftype set
        if getbufvar(buf, '&buftype') != ''
            continue
        endif

        " skip the preview window
        if getwinvar(i, '&previewwindow')
            continue
        endif

        " skip current window
        if i == winnr()
            continue
        endif

        return i
    endfor

    return -1
endfunction

" s:QuitIfOnlyWindow() {{{2
function! s:QuitIfOnlyWindow() abort
    let l:buftype = getbufvar(winbufnr(winnr()), "&buftype")
    if l:buftype != "quickfix" && l:buftype != "help"
        return
    endif

    " Check if there is more than one window
    if s:NextNormalWindow() == -1
        " Check if there is more than one tab page
        if tabpagenr('$') == 1
            " Before quitting Vim, delete the special buffer so that
            " the '0 mark is correctly set to the previous buffer.
            " Also disable autocmd on this command to avoid unnecessary
            " autocmd nesting.
            if winnr('$') == 1
                if has('autocmd')
                    noautocmd bdelete
                endif
            endif
            quit
        else
            " Note: workaround for the fact that in new tab the buftype is set
            " too late (and sticks during this WinEntry autocmd to the old -
            " potentially quickfix/help buftype - that would automatically
            " close the new tab and open the buffer in copen window instead
            " New tabpage has previous window set to 0
            if tabpagewinnr(tabpagenr(), '#') != 0
                let l:last_window = 0
                if winnr('$') == 1
                    let l:last_window = 1
                endif
                close
                if l:last_window == 1
                    " Note: workaround for the same bug, but w.r.t. Airline
                    " plugin (it needs to refresh buftype and status line after
                    " last special window autocmd close on a tab page
                    if exists(':AirlineRefresh')
                        execute "AirlineRefresh"
                    endif
                endif
            endif
        endif
    endif
endfunction

" autoclose last open location/quickfix/help windows on a tab
if has('autocmd')
    aug AutoCloseAllQF
        au!
        autocmd WinEnter * nested call s:QuitIfOnlyWindow()
    aug END
endif

这不会在快速修复窗口内打开新标签页时出现问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接