在打开“空白”的情况下触发Vim的自动命令

4

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

但是,使用vim newfile.txt仍应像正常情况下一样运行。

我该如何做?我似乎找不到正确的autocmd

2个回答

6

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

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

argc()函数返回在调用vim时在命令行上指定的文件名数量,除非有些东西修改了参数列表,在:h argc()中可以找到更多信息。


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

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

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