Reputation: 5953
I want vim to open up the :Explorer when no file is opened or created. Eg. when I call vim
without any options.
calling vim newfile.txt
should still behave the normal way though.
How would I go about doing this? I can't seem to find the correct autocmd
for it.
Upvotes: 4
Views: 890
Reputation: 53674
If you want to do this for vim invocation only, the best way is to use argc()
:
autocmd VimEnter * :if argc() is 0 | Explore | endif
argc()
function returns a number of filenames specified on command-line when vim was invoked unless something modified arguments list, more information at :h argc()
.
Upvotes: 6
Reputation: 5953
Found the answer myself:
"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
The greatest part of this code was taken from this question.
Upvotes: 2