Ignore node_modules with vim-fzf
Asked Answered
C

2

6

I have defined a function to search for filename (<C-P>), and a string (<C-F>) from git root directory asynchronously with fzf.vim plugin (I also have Ag installed). However, I can not manipulate the definition to ignore node_modules directory. The vim script is too hard to debug, there is no console to print anything.

Is there any expert in vim script that can help me sort this out. Many thanks in advance

let s:git_path = substitute(system("git rev-parse --show-toplevel 2>/dev/null"), '\n', '', '')

function! s:ag_git_root(query, ...)
  if type(a:query) != type('')
    return s:warn('Invalid query argument')
  endif
  let query = empty(a:query) ? '^(?=.)' : a:query
  let args = copy(a:000)
  let ag_opts = len(args) > 1 && type(args[0]) == s:TYPE.string ? remove(args, 0) : ''
  let command = ag_opts . ' ' . fzf#shellescape(query) . ' ' . s:git_path
  return call('fzf#vim#ag_raw', insert(args, command, 0))
endfunction

command! -bang -nargs=* A
      \ call s:ag_git_root(<q-args>, <bang>0)
command! -bang -nargs=? F
      \ call fzf#vim#files(s:git_path, <bang>0)
silent! nmap <C-P> :F<CR>
silent! nmap <C-F> :A<CR>
Concurrence answered 29/6, 2018 at 1:23 Comment(4)
ag honors your .gitignore by default so if you have node_modules there it should be ignored.Orelu
@Orelu it should, but actually it does notConcurrence
did you add node_modules/ to your .gitignore?Jumpoff
node_modules/ is already included in .gitignore, but it still is included in search result. I think fzf#vim#files# does not use ag` internally (I could not debug to see which is used) . Eventually, I figured out the solution with a minor demerit. check out my answer belowConcurrence
C
13

Finally, I figured out a workaround by replacing fzf#vim#files with :Gfiles

New configuration is silent! nmap <C-P> :GFiles<CR>

<C-F> mapping is kept the same as in the question.

The demerit of this :GFiles solution is that files not added to git (untracked files) are not included in the search results. They all can be added via git add . -A

The merit of this solution is that all files in .gitignore is ignored in the search results

Concurrence answered 21/7, 2018 at 14:45 Comment(2)
:GFiles takes arguments it will pass along to git-ls-files, so you can call have FZF list all your tracked files plus your untracked files minus your ignored files like this: :GFiles --exclude-standard --others --cachedUng
@NathanWallace for some reason the last command you gave is great but soooo sloowwwwww. I takes about 1sec for the files list to show. :GFiles alone does not.Mastermind
G
0

I was able to resolve this by defining the env var in the .bash_profile.

export FZF_DEFAULT_COMMAND='ag --nocolor --ignore node_modules -g ""'
Germinal answered 8/8, 2023 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.