我希望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