Load Vim Source Files Only If It Exists

For A Modular Vim Configuration

This is one of my favorite functions to use in vim configurations. You can load a vim file, but if it doesn't exist, fail silently.

This can be placd inside of ~/.vimrc:

" Function to source only if file exists {
function! SourceIfExists(file)
  if filereadable(expand(a:file))
    exe 'source' a:file
  endif
endfunction
" }

Here are some examples of my usage of it:

call SourceIfExists("~/.vim/colors.vim")
call SourceIfExists("~/.vimrc.local")

" FreeBSD-specific terminal fixes
if FREEBSD()
  call SourceIfExists("~/.vim/compat/freebsd.vim")
  call SourceIfExists("/usr/src/tools/tools/editing/freebsd.vim")
end

if has('gui_running')
  call SourceIfExists("~/.gvimrc.local")
endif

SourceIfExists() keeps things DRY while avoiding dense code there. For including all source files inside a directory, try the Include all vim files from directory snippet.