How to detect if a specific file exists in Vimscript?
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
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