4

我希望 vim 在没有打开或创建文件时打开 :Explorer。例如。当我打电话时vim没有任何选择。

呼叫vim newfile.txt应该仍然表现正常。

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

4

2 回答 2

6

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

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

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

于 2011-05-12T20:03:12.340 回答
2

自己找到了答案:

"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

这段代码的大部分内容来自这个问题

于 2011-05-12T12:01:59.790 回答