我希望 vim 在没有打开或创建文件时打开 :Explorer。例如。当我打电话时vim
没有任何选择。
呼叫vim newfile.txt
应该仍然表现正常。
我该怎么做呢?我似乎找不到正确autocmd
的方法。
如果您只想为 vim 调用执行此操作,最好的方法是使用argc()
:
autocmd VimEnter * :if argc() is 0 | Explore | endif
argc()
函数返回在调用 vim 时在命令行上指定的多个文件名,除非修改了参数列表,更多信息在:h argc()
.
自己找到了答案:
"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
这段代码的大部分内容来自这个问题。