【问题标题】:Unable to automatically source custom Vim files无法自动获取自定义 Vim 文件
【发布时间】:2014-02-05 10:25:56
【问题描述】:

这是我的确切设置的回购:https://github.com/Integralist/Fresh-Install/tree/master/Shell

这里是我的.vimrc 文件的链接:https://github.com/Integralist/Fresh-Install/blob/master/Shell/.vimrc

这是我的自定义 .vim 文件的专门链接:https://github.com/Integralist/Fresh-Install/tree/master/Shell/vim


我正在尝试自动获取一些自定义 .vim 文件。

为了使自动化工作,我意识到我需要我的自定义 .vim 文件按特定顺序排列,因此我必须在文件名前加上一个数字。比如1.settings.vim

我尝试将它们放在 .vim/plugin 中,但是当我打开 Vim 时,我的插件都没有运行,所以我必须手动执行 :BundleInstall(或在启动 Vim 之前运行 vim +BundleInstall)但是当我关闭 Vim 并开始它再次插件再次没有运行。因此,除非有办法让 Vim 自动执行 bundle install 命令(并且只执行一次,因为每次打开 Vim 都执行此操作太可笑了)。

因此,我决定尝试通过 Vim 脚本自己自动获取它们。

以下脚本有效...

set runtimepath+=$DROPBOX/Fresh\ Install/Shell/vim
runtime 1.settings.vim
runtime 2.vundle.vim
runtime 3.mapping.vim
runtime 4.commands.vim

...但不可扩展,因为每次我添加一个新的 .vim 文件时,我都必须更新脚本。

所以我尝试了另一种方法......

注意:我的 .zshrc 导出变量 DROPBOX 并将其设置为我的 Dropbox 路径。

如果我在新选项卡中打开 Vim(所以我在我的 ~/ 目录中)并执行 :echo $DROPBOX 我看到 /Users/markmcdonnell/Dropbox

如果我在新选项卡中打开 Vim(所以我在我的 ~/ 目录中)并执行 :echo isdirectory("$DROPBOX/") 我会返回 0 这意味着 Vim 无法找到该目录。

但是如果我在一个新选项卡中打开 Vim(所以我在我的 ~/ 目录中)并执行 :echo isdirectory("/Users/markmcdonnell/Dropbox/")(这与 :echo $DROPBOX 返回的内容相同,只是在最后)我得到一个1,这意味着 Vim 可以找到那个目录。

因此,Vim 脚本如何解释手动键入的路径和从环境变量扩展的路径有些不太正确。

我尝试的示例脚本是...

if isdirectory("$DROPBOX/Fresh Install/Shell/vim")
  for file in split(globpath('$DROPBOX/Fresh Install/Shell/vim/', '*.vim'), '\n')
    execute 'source ' file
  endfor
endif

...但是文件没有被获取,那时我意识到在 Vim 中 isdirectory("$DROPBOX/Fresh Install/Shell/vim") 行正在返回 0

我无法手动输入完整路径,因为我在不同的机器上同步了我的文件,因此路径发生了变化。

因此,我将 if 语句修改为:isdirectory(expand('%:p:h').'/vim'),它在 Vim 中运行时返回 1(这是当我将 cd 放入 Dropbox 文件夹 /Fresh Install/ 并打开 /Shell/.vimrc 文件时)。

所以,:echo expand('%:p:h') 返回/Users/markmcdonnell/Dropbox/Fresh Install/Shell,因此它已设法找到正确的目录。

但这又没用。

我还尝试了另一种格式(但基本上是相同的过程,只是写得更简洁):" execute join(map(split(glob("$DROPBOX/Fresh Install/Shell/vim/*.vim"), "\n"), '"source " . v:val'), "\n")

还是不行。

然后我尝试了......

for file in split(globpath('$DROPBOX/Fresh Install/Shell/vim/', '*.vim'), '\n')
  execute 'source ' . file
endfor

...但没有检查文件夹是否存在(因为它应该始终存在)。

但现在我得到了错误:Only one file name allowed: source /Users/markmcdonnell/Dropbox/Fresh Install/Shell/vim/1.settings.vim

所以看起来它几乎可以工作了,但路径中的空格可能导致脚本认为有两个参数,而实际上它只有一个。

我回到上一个(非简洁版)并尝试引用命令:

execute 'source ' . "file"execute 'source ' . "$file" 以及 execute "source $file"

但同样,这些都不起作用。

我确定我错过了一些非常简单的东西,但感谢任何帮助。

更新:

let files = split(glob("$DROPBOX/Fresh Install/Shell/vim/*.vim"), "\n")
let mapped = map(files, '"source " . v:val')
execute join(mapped, "\n")

这与之前的命令相同,但扩展了一点。

我想我需要能够在v:val 周围加上一组双引号,这样当命令为executed 时,文件名中的空格就不会导致问题。

更新 2:

这是运行:verbose set rtp?的结果

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

这是我删除以下sn-p代码后的结果...

set runtimepath+=$DROPBOX/Fresh\ Install/Shell/vim
runtime 1.settings.vim
runtime 2.vundle.vim
runtime 3.mapping.vim
runtime 4.commands.vim

...我用来手动获取 vim 文件。

值得注意的是,我在 ~/.vim/plugin 中有一个 BufOnly.vim 文件,它会自动加载,所以为什么其他 .vim 文件不是很奇怪

更新 3

这是运行:scriptnames的结果

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

再次,根据 UPDATE 2,这没有我在 .vimrc 文件中的任何代码,之前我手动获取了我单独的 vim 脚本文件。在运行 :scriptnames 时,我的 .vimrc 中的唯一代码是...

if filereadable(".vimlocal")
  source .vimlocal
endif

...但有趣的是,您可以在上面看到~/Dropbox/Fresh Install/Shell/vim/ 中的文件看起来正在加载(那是:scriptnames 显示的内容?)。请注意~/Dropbox/Fresh Install/Shell/vim/ 中的文件是~/.vim/plugin 中的文件的副本

【问题讨论】:

  • 你有没有试过在空格前加一个 \ 看看它是否有效?
  • 你的模块化vim配置文件看起来很整洁,但我担心它可能不方便日常使用和配置调整。 vimrc 通常不会超过 1000 行,大多数人不会达到 200 行。将它们放入 5 个以上的文件/模块中会有点复杂。如果你想拥有不同的配置文件,你可以创建不同的 vimrc 文件。我的 2 美分
  • @bilalq 是的,我试过了,但没有用。我在上面添加了一个更新。

标签: bash zsh vim


【解决方案1】:

假设$FOO 指向一个真实存在的目录……

:echo isdirectory("$FOO")

返回0,因为使用了字符串$FOO,而您没有$FOO 目录。

:echo isdirectory($FOO)

返回1,因为使用了$FOO 的值而不是字符串$FOO


我是否可以补充一点,我发现您的设置不必要地复杂?

【讨论】:

    【解决方案2】:

    我已经到了

    我尝试将它们放在 .vim/plugin 中,但是当我打开 Vim 时,我的插件都没有运行

    我宁愿修理爆胎也不愿重新发明轮子。

    .vim/plugin 是指~/.vim/plugin/,所以你的文件是

    ~/.vim/plugin/1.settings.vim
    

    等等?如果是这样,并且如果您使用的是类似 *NIX 的系统,那么这应该可以工作。请参阅:help load-plugins 了解 vim 可能会跳过加载插件文件的步骤的原因。试试:verbose set rtp? 看看你是否应该使用~/.vim 或其他东西。使用:scriptnames 确保它们没有被加载——对于您所看到的内容可能还有其他解释。

    更新(基于您的 cmets):

    我没想到~/.vim/plugin 会被列为'rtp' 的一部分。我确实希望~/.vim 会被列出,而且确实如此。根据:help load-plugins,添加了plugin/部分路径。

    你说BufOnly.vim来源于~/.vim/plugin/,但它被列为71: ~/Dropbox/Fresh Install/Shell/.vim/plugin/BufOnly.vim。你有没有从~/.vim~/Dropbox/Fresh Install/Shell/.vim/ 的符号链接?你想要的文件,1.settings.vim4.commands.vim,都在同一个目录中,并且它们也是来源,根据你从 :scriptnames 的输出(第 50 和 68-70 行,就在 BufOnly.vim 之前)。

    我在这里没有看到任何神秘的东西。您想要获取的文件正在中获取。如果它们没有达到您想要的效果,请在其他地方寻找解释。

    顺便说一下,1.settings.vim2.vundle.vim 之间大约有 17 个文件。我很确定1.settings.vim 必须触发这些其他文件。

    【讨论】:

    • 查看我上面的更新 2,它显示了运行时路径的内容, ~/.vim 插件文件夹根本没有被引用。
    • 另外值得注意的是,我在 ~/.vim/plugin 中有一个BufOnly.vim,并且该脚本是自动运行的,所以为什么其他脚本也没有自动获取,这使得它更加陌生
    • 在 UPDATE 2 中添加了一条注释以阐明我在做什么
    • 现在有一个更新 3 与 :scriptnames 结果
    【解决方案3】:

    我遇到了类似的问题。 〜/.vim/plugin 无法识别符号链接,因此您必须将文件复制到那里,而不是创建指向包含所有文件的“git”文件夹的符号链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-05
      • 2022-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多