打开时Vim自动命令触发器“;“什么都没有”;

打开时Vim自动命令触发器“;“什么都没有”;,vim,autocmd,Vim,Autocmd,我希望vim在没有打开或创建任何文件时打开:Explorer。例如,当我在没有任何选项的情况下调用vim时 调用vim newfile.txt应仍然以正常方式进行 我该怎么做呢?我似乎找不到正确的autocmd。自己找到了答案: "open to Explorer when no file is opened function! TabIsEmpty() " Remember which window we're in at the moment let initial_win_

我希望vim在没有打开或创建任何文件时打开:Explorer。例如,当我在没有任何选项的情况下调用
vim

调用vim newfile.txt应仍然以正常方式进行


我该怎么做呢?我似乎找不到正确的
autocmd

自己找到了答案:

"open to Explorer when no file is opened
function! TabIsEmpty()
    " Remember which window we're in at the moment
    let initial_win_num = winnr()

    let win_count = 0
    " Add the length of the file name on to count:
    " this will be 0 if there is no file name
    windo let win_count += len(expand('%'))

    " Go back to the initial window
    exe initial_win_num . "wincmd w"

    " Check count
    if win_count == 0
        " Tab page is empty
        return 1
    else
        return 0
    endif
endfunction

" Test it like this:
" echo TabIsEmpty()

function! OpenExplorer()
    if (TabIsEmpty())
        :Explore
    end  
endfunction

此代码的大部分来自。

如果您只想为vim调用执行此操作,最好的方法是使用
argc()

argc()
函数返回调用vim时在命令行上指定的许多文件名,除非有修改的参数列表,更多信息请参见
:h argc()

autocmd VimEnter * :if argc() is 0 | Explore | endif