2

I want to reorganization my vim directory to be like this:

- dotfiles
  |- vim
  |  |- config
  |  |  |- global
  |  |  |  |- basic.vim
  |  |  |  |...
  |  |  |- plugins
  |  |  |  |- ctrl-p.vim
  |  |  |  |...
  |  |  |- functions
  |  |  |  |...
  |  |  |...
  |  |- bootstrap.vimrc

in $HOME/.vimrc:

if isdirectory(expand('$HOME/dotfiles/vim'))
  source $HOME/dotfiles/vim/bootstrap.vimrc
endif

My question is, how to include all Vim config files in global, plugins, functions folder?

I find a function like this:

function! SourceConfig()
  let file_list = split(globpath('$HOME/dotfiles/vim/', '.vim'), '\n')

  for file in file_list
    execute('source '.file)
  endfor
endfunction

call sourceConfig()

But I want to according to the specified load folder, how to modify the func, make the globpath change to be an array or something, like below.

let $path=[global, plugins, functions] // this will be source files in turn.

function! SourceConfig()
  let file_list = split(globpath($path, '.vim'), '\n')

  for file in file_list
    execute('source '.file)
  endfor
endfunction

call sourceConfig()

Use before/plugin and after/plugin?

1 Answer 1

4

As globpath() takes a comma-separated list of directories, you can build the locations like this (I'm doing this in a primitive way with duplication, as you seem to be not very well versed in Vimscript):

function! sourceConfig()
  let path = $HOME  . '/dotfiles/vim/config/global'
  let path .= ',' . $HOME  . '/dotfiles/vim/config/plugins'
  let path .= ',' . $HOME  . '/dotfiles/vim/config/functions'

Then, this list can be passed to generate the list of script files, which can then be sourced (note that your syntax was wrong, and it's recommended to do proper escaping):

  let file_list = split(globpath(path, '*.vim'), '\n')

  for file in file_list
    execute 'source' fnameescape(file)
  endfor
endfunction

Critique of approach

You didn't mention the motivation for that reorganization, but I would recommend to stick to default configuration layouts, either the "all under .vim" or the "separate bundle directories per plugin".

For example, your listed approach doesn't handle ftplugins or autoload, which I'm sure some of your plugins has / will have.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .