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>
Sang
  • 4,049
  • 3
  • 37
  • 47
  • `ag` honors your `.gitignore` by default so if you have `node_modules` there it should be ignored. – romainl Jun 29 '18 at 06:12
  • @romainl it should, but actually it does not – Sang Jun 29 '18 at 06:54
  • did you add node_modules/ to your .gitignore? – gregory Jul 21 '18 at 03:24
  • 1
    `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 below – Sang Jul 21 '18 at 14:40

2 Answers2

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

Sang
  • 4,049
  • 3
  • 37
  • 47
  • 7
    `: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 --cached` – Nathan Wallace Feb 19 '19 at 17:02
  • @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. – Brandon Jul 12 '20 at 23:49
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 ""'
ruslan
  • 909
  • 6
  • 7