title: Unable to automatically source custom Vim files
body: <p>Here is a repo with my exact set-up: <a href="https://github.com/Integralist/Fresh-Install/tree/master/Shell" rel="nofollow">https://github.com/Integralist/Fresh-Install/tree/master/Shell</a></p>
  
  <p>Here is the link specifically to my <code>.vimrc</code> file: <a href="https://github.com/Integralist/Fresh-Install/blob/master/Shell/.vimrc" rel="nofollow">https://github.com/Integralist/Fresh-Install/blob/master/Shell/.vimrc</a></p>
  
  <p>Here is the link specifically to my custom <code>.vim</code> files: <a href="https://github.com/Integralist/Fresh-Install/tree/master/Shell/vim" rel="nofollow">https://github.com/Integralist/Fresh-Install/tree/master/Shell/vim</a></p>
  
  <hr>
  
  <p>I'm trying to automatically source some custom <code>.vim</code> files.</p>
  
  <p>For automation to work I realised I needed my custom <code>.vim</code> files to be in a specific order and so I had to prefix the file names with a numerical number. For example, <code>1.settings.vim</code></p>
  
  <p>I tried putting them inside of <code>.vim/plugin</code> but when I open Vim none of my plugins are running so I have to manually execute <code>:BundleInstall</code> (or before starting Vim run <code>vim +BundleInstall</code>) but the moment I close Vim and start it again the plugins again are not running. So unless there is a way to get Vim to automatically execute the bundle install command (and only do it once, because doing it every time I open Vim is just ridiculous).</p>
  
  <p>So instead, I decided to try and automatically source them myself via Vim Script.</p>
  
  <p>The following script works...</p>
  
  <pre><code>set runtimepath+=$DROPBOX/Fresh\ Install/Shell/vim
  runtime 1.settings.vim
  runtime 2.vundle.vim
  runtime 3.mapping.vim
  runtime 4.commands.vim
  </code></pre>
  
  <p>...but isn't scalable because every time I add a new <code>.vim</code> file then I'll have to update the script.</p>
  
  <p>So I tried another way...</p>
  
  <p>Note: my <code>.zshrc</code> exports the variable <code>DROPBOX</code> and sets it to my Dropbox path.</p>
  
  <p>If I open up Vim in a new tab (so I'm in my <code>~/</code> directory) and execute <code>:echo $DROPBOX</code> I see <code>/Users/markmcdonnell/Dropbox</code>.</p>
  
  <p>If I open up Vim in a new tab (so I'm in my <code>~/</code> directory) and execute <code>:echo isdirectory("$DROPBOX/")</code> I get back <code>0</code> which means Vim cannot locate that directory.</p>
  
  <p>But if I open up Vim in a new tab (so I'm in my <code>~/</code> directory) and execute <code>:echo isdirectory("/Users/markmcdonnell/Dropbox/")</code> (which is the same as what <code>:echo $DROPBOX</code> returned with the addition of an extra forward slash at the end) I get back a <code>1</code> which means Vim <em>CAN</em> locate that directory.</p>
  
  <p>So there is something not quite right with how Vim Script interprets a manually typed path and one that is expanded from an environment variable.</p>
  
  <p>An example script I tried was...</p>
  
  <pre><code>if isdirectory("$DROPBOX/Fresh Install/Shell/vim")
    for file in split(globpath('$DROPBOX/Fresh Install/Shell/vim/', '*.vim'), '\n')
      execute 'source ' file
    endfor
  endif
  </code></pre>
  
  <p>...but the files weren't being sourced and that's when I realised that within Vim the line <code>isdirectory("$DROPBOX/Fresh Install/Shell/vim")</code> was returning <code>0</code>.</p>
  
  <p>I can't manually enter the full path because I sync my files across different machines and so the path changes.</p>
  
  <p>So instead I modified the if statement to: <code>isdirectory(expand('%:p:h').'/vim')</code> which returns <code>1</code> when run within Vim (this is when I <code>cd</code> into the Dropbox folder <code>/Fresh Install/</code> and open the <code>/Shell/.vimrc</code> file).</p>
  
  <p>So, <code>:echo expand('%:p:h')</code> returns <code>/Users/markmcdonnell/Dropbox/Fresh Install/Shell</code> hence it has managed to locate the correct directory.</p>
  
  <p>But again this did not work.</p>
  
  <p>I also tried another format (but essentially the same process just more concisely written): <code>"   execute join(map(split(glob("$DROPBOX/Fresh Install/Shell/vim/*.vim"), "\n"), '"source " . v:val'), "\n")
  </code></p>
  
  <p>Again did not work.</p>
  
  <p>I then tried...</p>
  
  <pre><code>for file in split(globpath('$DROPBOX/Fresh Install/Shell/vim/', '*.vim'), '\n')
    execute 'source ' . file
  endfor
  </code></pre>
  
  <p>...but without checking the folder existed (because it should always exist). </p>
  
  <p>But now I get the error: <code>Only one file name allowed: source /Users/markmcdonnell/Dropbox/Fresh Install/Shell/vim/1.settings.vim</code></p>
  
  <p>So it looks like it's almost working but maybe the space in the path is causing the script to think there are two arguments when it's really just one.</p>
  
  <p>I went back to the previous (non concise version) and tried to quote the command:</p>
  
  <p><code>execute 'source ' . "file"</code> and <code>execute 'source ' . "$file"</code> and also <code>execute "source $file"</code></p>
  
  <p>But again, none of these worked.</p>
  
  <p>I'm sure I'm missing something really simple, but any help appreciated.</p>
  
  <h2>UPDATE:</h2>
  
  <pre><code>let files = split(glob("$DROPBOX/Fresh Install/Shell/vim/*.vim"), "\n")
  let mapped = map(files, '"source " . v:val')
  execute join(mapped, "\n")
  </code></pre>
  
  <p>This is the same command as before but expanded a little bit.</p>
  
  <p>I think I need to be able to get a set of double quotes around the <code>v:val</code> so that when the command is <code>execute</code>d then the space in the file name wont cause an issue.</p>
  
  <h2>UPDATE 2:</h2>
  
  <p>Here is the result of running <code>:verbose set rtp?</code></p>
  
  <pre><code>runtimepath=
  ~/.vim/bundle/vundle
  ~/.vim/bundle/ctrlp.vim
  ~/.vim/bundle/tomorrow-night-vim
  ~/.vim/bundle/vim-markdown
  ~/.vim/bund le/vim-cucumber
  ~/.vim/bundle/vim-misc
  ~/.vim/bundle/tagbar
  ~/.vim/bundle/YouCompleteMe
  ~/.vim/bundle/vim-repeat
  ~/.vim/bundle/vim -commentary
  ~/.vim/bundle/ack.vim
  ~/.vim/bundle/vim-endwise
  ~/.vim/bundle/vim-airline
  ~/.vim/bundle/tmuxline.vim
  ~/.vim/bundle/web api-vim
  ~/.vim/bundle/Gist.vim
  ~/.vim/bundle/vim-fugitive
  ~/.vim/bundle/emmet-vim
  ~/.vim/bundle/rename.vim
  ~/.vim/bundle/vim-gitgu tter
  ~/.vim/bundle/nerdtree
  ~/.vim/bundle/vim-haml
  ~/.vim/bundle/vim-surround
  ~/.vim/bundle/html5.vim
  ~/.vim/bundle/tabular
  ~/.vim /bundle/camelcasemotion
  ~/.vim
  /usr/local/share/vim/vimfiles
  /usr/local/share/vim/vim74
  /usr/local/share/vim/vimfiles/after
  ~/.vim /after
  ~/Dropbox/Fresh Install/Shell/vim
  ~/.vim/bundle/vundle/after
  ~/.vim/bundle/ctrlp.vim/after
  ~/.vim/bundle/tomorrow-night-vim
  /after
  ~/.vim/bundle/vim-markdown/after
  ~/.vim/bundle/vim-cucumber/after
  ~/.vim/bundle/vim-misc/after
  ~/.vim/bundle/tagbar/after
  
  Last set from ~/Dropbox/Fresh Install/Shell/.vim/bundle/vundle/autoload/vundle/config.vim
  </code></pre>
  
  <p>This was the result after I removed the following snippet of code...</p>
  
  <pre><code>set runtimepath+=$DROPBOX/Fresh\ Install/Shell/vim
  runtime 1.settings.vim
  runtime 2.vundle.vim
  runtime 3.mapping.vim
  runtime 4.commands.vim
  </code></pre>
  
  <p>...which I'm using to manually source the vim files.</p>
  
  <p>It's worth noting as well that I have a <code>BufOnly.vim</code> file within <code>~/.vim/plugin</code> that is being automatically loaded so why the other <code>.vim</code> files aren't is very odd</p>
  
  <h2>UPDATE 3</h2>
  
  <p>Here is the result of running <code>:scriptnames</code></p>
  
  <pre><code>1: ~/.vimrc
  2: ~/Dropbox/Fresh Install/Shell/vim/1.settings.vim
  3: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/syntax.vim
  4: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/synload.vim
  5: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/syncolor.vim
  6: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/filetype.vim
  7: ~/Dropbox/Fresh Install/Shell/vim/2.vundle.vim
  8: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/ftoff.vim
  9: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vundle/autoload/vundle.vim
  10: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vundle/autoload/vundle/config.vim
  11: ~/Dropbox/Fresh Install/Shell/.vim/bundle/tomorrow-night-vim/colors/tomorrow-night-bright.vim
  12: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-markdown/ftdetect/markdown.vim
  13: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-cucumber/ftdetect/cucumber.vim
  14: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-haml/ftdetect/haml.vim
  15: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/ftplugin.vim
  16: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/indent.vim
  17: ~/Dropbox/Fresh Install/Shell/vim/3.mapping.vim
  18: ~/Dropbox/Fresh Install/Shell/vim/4.commands.vim
  19: ~/Dropbox/Fresh Install/Shell/.vim/bundle/ctrlp.vim/plugin/ctrlp.vim
  20: ~/Dropbox/Fresh Install/Shell/.vim/bundle/ctrlp.vim/autoload/ctrlp/mrufiles.vim
  21: ~/Dropbox/Fresh Install/Shell/.vim/bundle/tagbar/plugin/tagbar.vim
  22: ~/Dropbox/Fresh Install/Shell/.vim/bundle/YouCompleteMe/plugin/youcompleteme.vim
  23: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-commentary/plugin/commentary.vim
  24: ~/Dropbox/Fresh Install/Shell/.vim/bundle/ack.vim/plugin/ack.vim
  25: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-endwise/plugin/endwise.vim
  26: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/plugin/airline.vim
  27: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline.vim
  28: ~/Dropbox/Fresh Install/Shell/.vim/bundle/tmuxline.vim/plugin/tmuxline.vim
  29: ~/Dropbox/Fresh Install/Shell/.vim/bundle/Gist.vim/plugin/gist.vim
  30: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-fugitive/plugin/fugitive.vim
  31: ~/Dropbox/Fresh Install/Shell/.vim/bundle/emmet-vim/plugin/emmet.vim
  32: ~/Dropbox/Fresh Install/Shell/.vim/bundle/rename.vim/plugin/rename.vim
  33: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-gitgutter/plugin/gitgutter.vim
  34: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/plugin/NERD_tree.vim
  35: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/autoload/nerdtree.vim
  36: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/path.vim
  37: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/menu_controller.vim
  38: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/menu_item.vim
  39: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/key_map.vim
  40: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/bookmark.vim
  41: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/tree_file_node.vim
  42: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/tree_dir_node.vim
  43: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/opener.vim
  44: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/lib/nerdtree/creator.vim
  45: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/nerdtree_plugin/exec_menuitem.vim
  46: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/nerdtree_plugin/fs_menu.vim
  47: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-surround/plugin/surround.vim
  48: ~/Dropbox/Fresh Install/Shell/.vim/bundle/tabular/plugin/Tabular.vim
  49: ~/Dropbox/Fresh Install/Shell/.vim/bundle/camelcasemotion/plugin/camelcasemotion.vim
  50: ~/Dropbox/Fresh Install/Shell/.vim/plugin/1.settings.vim
  51: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/nosyntax.vim
  52: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/init.vim
  53: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/parts.vim
  54: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/extensions.vim
  55: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/extensions/quickfix.vim
  56: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/extensions/ctrlp.vim
  57: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/extensions/hunks.vim
  58: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/extensions/tagbar.vim
  59: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/extensions/branch.vim
  60: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/extensions/whitespace.vim
  61: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/section.vim
  62: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/highlighter.vim
  63: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/themes/tomorrow.vim
  64: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/themes.vim
  65: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/builder.vim
  66: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/util.vim
  67: ~/Dropbox/Fresh Install/Shell/.vim/bundle/vim-airline/autoload/airline/extensions/default.vim
  68: ~/Dropbox/Fresh Install/Shell/.vim/plugin/2.vundle.vim
  69: ~/Dropbox/Fresh Install/Shell/.vim/plugin/3.mapping.vim
  70: ~/Dropbox/Fresh Install/Shell/.vim/plugin/4.commands.vim
  71: ~/Dropbox/Fresh Install/Shell/.vim/plugin/BufOnly.vim
  72: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/getscriptPlugin.vim
  73: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/gzip.vim
  74: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/matchparen.vim
  75: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/netrwPlugin.vim
  76: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/rrhelper.vim
  77: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/spellfile.vim
  78: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/tarPlugin.vim
  79: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/tohtml.vim
  80: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/vimballPlugin.vim
  81: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/plugin/zipPlugin.vim
  82: ~/Dropbox/Fresh Install/Shell/.vim/bundle/tabular/after/plugin/TabularMaps.vim
  83: ~/Dropbox/Fresh Install/Shell/.vim/bundle/tabular/autoload/tabular.vim
  84: ~/Dropbox/Fresh Install/Shell/.vim/bundle/YouCompleteMe/autoload/youcompleteme.vim
  85: ~/Dropbox/Fresh Install/Shell/.vim/bundle/tagbar/autoload/tagbar.vim
  86: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/scripts.vim
  87: ~/Dropbox/Fresh Install/Shell/.vim/bundle/nerdtree/syntax/nerdtree.vim
  88: ~/Dropbox/Fresh Install/Shell/.vim/bundle/ctrlp.vim/autoload/ctrlp/utils.vim
  89: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/ftplugin/vim.vim
  90: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/indent/vim.vim
  91: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/vim.vim
  92: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/perl.vim
  93: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/pod.vim
  94: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/ruby.vim
  95: /usr/local/Cellar/vim/7.4.052/share/vim/vim74/syntax/python.vim
  </code></pre>
  
  <p>Again, as per UPDATE 2 this is without any of the code I had in my <code>.vimrc</code> file before that was manually sourcing my separate vim script files. The only code in my <code>.vimrc</code> at the point of running <code>:scriptnames</code> was...</p>
  
  <pre><code>if filereadable(".vimlocal")
    source .vimlocal
  endif
  </code></pre>
  
  <p>...but interestingly you can see above that the files within <code>~/Dropbox/Fresh Install/Shell/vim/</code> look to be loading (that's if that is what <code>:scriptnames</code> is showing?). Note that the files within <code>~/Dropbox/Fresh Install/Shell/vim/</code> are a copy of the files within <code>~/.vim/plugin</code></p>