With a bit of searching in vim man
I've found this, which looks much better that the original:
:function! SomeCheck()
: if filereadable("SpecificFile")
: echo "SpecificFile exists"
: endif
:endfunction
Some of the comments express concerns about filereadable
and using glob
instead. This addresses the issue of having a file that does exist, but permissions prevent it from being read. If you want to detect such cases, the following will work:
:if !empty(glob("path/to/file"))
: echo "File exists."
:endif
Giving some more visibility to metaphy's comment on the accepted answer:
if filereadable(expand("~/.vim/bundle/vundle/README.md")) let g:hasVundle = 1 endif
filereadable
is what is required, but there's an extra handy step of expand
, should you be using ~
in your path:
:function! SomeCheck()
: if filereadable(expand("SpecificFile"))
: echo "SpecificFile exists"
: endif
:endfunction
For example
:echo filereadable('~/.vimrc')
gives 0
, :echo filereadable(expand('~/.vimrc'))
gives 1
Sorry if it's too late, but doing
if !empty(expand(glob("filename")))
echo "File exists"
else
echo "File does not exists"
endif
works fine for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With