Load vim source files only if they exist
for a modular vim configuration
vimscript vim
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 endifendfunction" }Here are some examples of my usage of it:
call SourceIfExists("~/.vim/colors.vim")call SourceIfExists("~/.vimrc.local")
" FreeBSD-specific terminal fixesif 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")endifSourceIfExists() keeps things
DRY while
avoiding dense code there. For including all source files inside a
directory, try the Include all vim files in a
directory snippet.