我可以让vim尊重我的.gitignore文件吗? [英] Can I make vim respect my .gitignore files?

查看:75
本文介绍了我可以让vim尊重我的.gitignore文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering if there is a way to get vim to read .gitignore files and use them to determine options not to present when auto-completing filenames.

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

For example, working in python, I'd like to not see .pyc files offered for editing. I think vim has its own mechanism for this, I was wondering how to load information from .gitignore into it.

推荐答案

如@dwc所建议,这是一个vim脚本:

As suggested by @dwc, here's a vim script:

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的格式,然后设置该选项.

Take that source and put it in a file in your plugin directory, such as ~/.vim/plugin/gitignore.vim. It will read your .gitignore file and parse it, transforming its format into one suitable for wildignore, and then set that option.

限制:

  • 这将从启动vim的目录中读取.gitignore文件.不会寻找其他.gitignore文件并对其进行解析.或者,您可以在第一行中指定文件的绝对路径.
  • vim中的wildignore选项不支持求反忽略的概念,就像在.gitignore文件中一样.也就是说,您不能说:set wildignore=*.html,!foo.html让它忽略除foo.html之外的所有HTML文件.因此,以.gitignore开头的行!只会被忽略.
  • This will read the .gitignore file from the directory where you launch vim. No effort is made to look for other .gitignore files and parse them. Alternatively, you could specify an absolute path to a file on the first line.
  • The wildignore option in vim doesn't support the notion of negating ignores like you can in a .gitignore file. That is, you can't say :set wildignore=*.html,!foo.html to have it ignore all html files except foo.html. Therefore, .gitignore lines that start with ! are simply ignored.

这篇关于我可以让vim尊重我的.gitignore文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆