Can I make vim respect my .gitignore files?

前端 未结 3 730
时光取名叫无心
时光取名叫无心 2021-02-05 15:16

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.

For example, work

3条回答
  •  孤街浪徒
    2021-02-05 16:00

    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
    

    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.

    Limitations:

    • 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.

提交回复
热议问题