我可以让 vim 尊重我的 .gitignore 文件吗?

2024-04-06

我想知道是否有一种方法可以让 vim 读取 .gitignore 文件并使用它们来确定自动完成文件名时不显示的选项。

例如,在 python 中工作,我不想看到可供编辑的 .pyc 文件。我认为 vim 有它自己的机制,我想知道如何将 .gitignore 中的信息加载到其中。


根据@dwc的建议,这是一个vim脚本:

let filename = '.gitignore'
if filereadable(filename)
    let igstring = ''
    for oline in readfile(filename)
        let line = substitute(oline, '\s|\n|\r', '', "g")
        if line =~ '^#' | con | endif
        if line == '' | con  | endif
        if line =~ '^!' | con  | endif
        if line =~ '/$' | let igstring .= "," . line . "*" | con | endif
        let igstring .= "," . line
    endfor
    let execstring = "set wildignore=".substitute(igstring, '^,', '', "g")
    execute execstring
endif

获取该源并将其放入插件目录中的文件中,例如~/.vim/plugin/gitignore.vim。它会读取你的.gitignore文件并解析它,将其格式转换为适合的格式wildignore,然后设置该选项。

局限性:

  • 这将读取.gitignore从启动 vim 的目录中获取文件。没有努力寻找其他.gitignore文件并解析它们。或者,您可以在第一行指定文件的绝对路径。
  • The wildignorevim 中的选项不支持以下概念negating像你一样忽略.gitignore文件。也就是说,你不能说:set wildignore=*.html,!foo.html让它忽略所有 html 文件,除了foo.html。所以,.gitignore以 ! 开头的行就被简单地忽略了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以让 vim 尊重我的 .gitignore 文件吗? 的相关文章

随机推荐