How to detect if a specific file exists in Vimscript?

stefanB picture stefanB · Jun 23, 2010 · Viewed 39k times · Source

I'm looking for an elegant way in Vimscript to check if a file exists in the current directory.

I came up with the code below but I'm not sure if that's the most elegant solution (I'll set a Vim option if the file exists). Is there any way of not having to do another comparison of the filename?

Maybe use a different built-in function from Vim?

:function! SomeCheck()
:   if findfile("SpecificFile", ".") == "SpecificFile"
:       echo "SpecificFile exists"
:   endif
:endfunction

Answer

stefanB picture stefanB · Jun 23, 2010

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