How to source all vim files in directory

孤街醉人 提交于 2019-12-20 09:16:25

问题


I have split my .vimrc into several files and placed them into ~/vimfiles/vimrc.d/.

Currently I source each file in that directory using exact name:

source ~/vimfiles/vimrc.d/file1.vim
source ~/vimfiles/vimrc.d/file2.vim

etc.

How to make a loop thourgh all files in that directory so i could only have to do such loop in my .vimrc:

for file in ~/vimfiles/vimrc.d/*.vim
   source file
enfor

回答1:


As mb14 has already said, if you put them in ~/.vim/plugin they will be sourced automatically. For information, however, if you want to source all of the files in your vimrc.d directory, you could do this (requires a relatively recent Vim):

for f in split(glob('~/vimfiles/vimrc.d/*.vim'), '\n')
    exe 'source' f
endfor

You may also be interested in the autoload mechanism, described in :help 41.15: if you're defining a lot of functions, this can make start-up a bit quicker as the functions are only loaded the first time they're used.




回答2:


You can just put your files in the plugins directory (~/.vim/plugin). They will be automatically loaded.




回答3:


mb14 gave you the best answer. You want something automatically executed? Then use the standard organization: here the plugin/ subdirectory.

Otherwise, :runtime would have been your friend:

:runtime! vimrc.d/*.vim
  • :source barks when its parameter doesn't exist while :runtime silently source nothing.
  • :source can source only one file while :runtime! can source any number of files.
  • :source takes an absolute pathname, or a pathname relative to the current directory while :runtime takes a pathname relative to the 'runtimepath' option, which shouldn't be a problem as long as you respect vim conventions.



回答4:


The example from DrAl did not work for me, this is how I achieved it:

for fpath in split(globpath('~/.vimrc.d/', '*.vim'), '\n')
  exe 'source' fpath
endfor


来源:https://stackoverflow.com/questions/4500748/how-to-source-all-vim-files-in-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!