Vim自动命令触发器打开“无”

时间:2011-05-12 10:58:58

标签: vim autocmd

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

调用vim newfile.txt仍然应该以正常方式运行。

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

2 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分: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

此代码的最重要部分取自this question