Include all vim files in a directory
for a modular vim configuration
This is very similar to Load vim source files only if they exist. I use both of these in my vim configuration, which borrows from a lot of places. In some cases, much like this, it's difficult to tell for sure where this originated, but it's also simple enough it's OK to post.
" Function to source all .vim files in directory {
function! SourceDirectory(file)
for s:fpath in split(globpath(a:file, '*.vim'), '\n')
exe 'source' s:fpath
endfor
endfunction
" }
The above can be placed in ~/.vimrc
or any place that's executed
before code implementing it.
Here are some usage examples:
First, make a directory at ~/.vim/settings/
and make some .vim files
there:
$ cd ~/.vim
$ mkdir settings
$ touch settings/
$ curl -o settings/sensible.vim https://raw.githubusercontent.com/tpope/vim-sensible/master/plugin/sensible.vim
tpope/vim-sensible is used in this example. I asked for permission to use it under a permissive license at tpope/vim-sensible#106.
Inside of ~/.vimrc
, or anywhere after
function! SourceDirectory(file)
:
call SourceDirectory('~/.vim/settings')
The SourceDirectory
function, like
SourceIfExists()
keeps things DRY and things a bit more future
proof when creating a modular config.