将vim语法文件保存在与文档相同的目录中

时间:2014-11-08 23:43:41

标签: syntax-highlighting vim

我在笔记本电脑上的控制台vim中记录笔记,并且 我想按顺序为我的笔记添加语法高亮 加强他们。但是,我不想添加一个 每个知识领域有数百万种不同的文件类型 (例如我对编译器的注释会有所不同 关键词比我在FHS上的笔记),我也想 让您轻松分享这些笔记。做了一些 研究,我发现我可以得到我的行为 想要,但它似乎不是一个非常优雅的解决方案。 我在.vimrc添加了以下几行:

if (filereadable("./.custom_syntax.vim")
  let mysyntaxfile = "./.custom_syntax.vim"
  syntax enable
else
  syntax enable
endif

我真的不喜欢这个解决方案,因为它仍然存在 要求我让他们修改他们的.vimrc,但是我 怀疑没有改变就没办法做到这一点 他们系统上的任何东西另外,如果我有 目录中的文件不是笔记,vim仍然是 使用.custom_syntax.vim文件突出显示它们,因为 我不知道文件类型是什么。

有没有更好的方法来实现这个目标?

1 个答案:

答案 0 :(得分:0)

我不是使用旧的mysyntaxfile变量,而是:syntax enable(一次),然后是:source语法文件。您可以定义:autocmd,查找原始文件旁边的同名Vimscript文件:

" Automatically source an eponymous <file>.vim or <file>.<ext>.vim if it exists, as a bulked-up
" modeline and to provide file-specific customizations.
function! s:AutoSource()
    let l:testedScripts = [expand('<afile>') . '.vim']
    if expand('<afile>:e') !=# 'vim'
        " Don't source the edited Vimscript file itself.
        call add(l:testedScripts, expand('<afile>:r') . '.vim')
    endif

    for l:filespec in l:testedScripts
        if filereadable(l:filespec)
            execute 'source' fnameescape(l:filespec)
        endif
    endfor
endfunction
augroup AutoSource
    autocmd! BufNewFile,BufRead * call <SID>AutoSource()
augroup END

但是,在Vim配置中确实需要这样或类似的东西。