Skip to content

Instantly share code, notes, and snippets.

@scrooloose
Created May 2, 2015 21:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scrooloose/0cdccd1171891caf0e24 to your computer and use it in GitHub Desktop.
Save scrooloose/0cdccd1171891caf0e24 to your computer and use it in GitHub Desktop.
"Chuck this in [a vim runtime]/nerdtree_plugin/gitignore_filter.vim
if exists("loaded_nerdtree_gitignore_filter")
finish
endif
let loaded_nerdtree_gitignore_filter = 1
call NERDTreeAddPathFilter('NERDTreeGitIgnoreFilter')
function NERDTreeGitIgnoreFilter(params)
let fname = a:params['nerdtree'].root.path.str() . '/.gitignore'
if !filereadable(fname)
return
endif
return a:params['path'].str() =~ g:GitIgnoreRegex(fname)
endfunction
"convert the gitignore file into a regex that we can match filenames against
function g:GitIgnoreRegex(fname)
"the regex is expensive to build so we cache it
if exists('b:NERDTreeGitIgnoreRegex')
return b:NERDTreeGitIgnoreRegex
endif
let lines = readfile(a:fname)
let regexes = []
for l in lines
if l =~ '^#' || l =~ '^\s*$'
continue
endif
let regex = l
let regex = substitute(regex, '\.', '\\.', 'g')
let regex = substitute(regex, '*', '.*', 'g')
let regex = substitute(regex, '?', '.', 'g')
let regex = escape(regex, '/~')
call add(regexes, regex)
endfor
let b:NERDTreeGitIgnoreRegex = '\(' . join(regexes, '\|') . '\)$'
return b:NERDTreeGitIgnoreRegex
endfunction
@albfan
Copy link

albfan commented May 9, 2015

change line 44 with

let regexes = g:GitIgnoreRegex(a:fname)
if regexes == '\(\)$'
    return
endif
return a:params['path'].str() =~ regexes

to avoid filter all files with files without regex matches

@xanderdunn
Copy link

Super useful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment