include all vim files in a folder

3

For my .bashrc I have a lot of small snippet files in .config/bash, like 10-prompt.sh and so on. In my actual .bashrc, I just have the following:

configdir="$HOME/.config/bash"

for file in "$configdir"/*.sh
do
        source "$file"
done

I'd like to do the same for my .vimrc, but I am not that confident in VimL that I could write that.

How would the snippet for .vimrc look like that includes all the snippets in a given subfolder? Ideally, I'd like to make a .vim/rc/ folder where I can put my snippets into.

Martin Ueding

Posted 2012-06-18T09:02:02.943

Reputation: 1 857

Answers

2

EDIT

This is @queueoverflow's shorter version:

for rcfile in split(globpath("~/.vim/rc", "*.vim"), '\n') 
    execute('source '.rcfile)
endfor

ENDEDIT

EDIT

(removed my stupid answer)

The function below seems to work.

function! SourceMyScripts()
  let file_list = split(globpath("~/.vim/rc", "*.vim"), '\n')

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

Add it to your ~/.vimrc like that:

call SourceMyScripts()

ENDEDIT

romainl

Posted 2012-06-18T09:02:02.943

Reputation: 19 227

That does not do anything :-/ – Martin Ueding – 2012-06-18T10:01:21.957

Yes, that's an horribly stupid answer. – romainl – 2012-06-18T11:51:32.837

So? Do you know the correct answer or what I did wrong then? – Martin Ueding – 2012-06-18T12:47:04.027

No, my answer was very stupid. I'm currently trying my hands at a solution. I'm not a vimscript wizard, though. – romainl – 2012-06-18T12:48:39.950

Thank you for trying! Currently, I just made cat ~/.vim/rc/*.vim > ~/.vimrc as a workaround, but if Vim was able to do this automatically, that would be even cooler! – Martin Ueding – 2012-06-18T12:51:18.623

See my edit. Tell me if it works for you. – romainl – 2012-06-18T13:11:38.447

It works, I edited the post since it can be written even shorter. – Martin Ueding – 2012-06-18T13:16:16.363

Thanks but you shouldn't completely rewrite answers like that. Instead, add the definitive solution as an answer or amend your question. – romainl – 2012-06-18T13:31:41.737

0

A very easy way to do this is ":runtime rc/*.vim". Of course, this would iterate over each "rc" subdirectory that might exist in each of the directories in 'runtimepath', but I doubt that will be a problem.

See:

:help :runtime
:help 'runtimepath'

Heptite

Posted 2012-06-18T09:02:02.943

Reputation: 16 267