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>
4
  • ag honors your .gitignore by default so if you have node_modules there it should be ignored.
    – romainl
    Commented Jun 29, 2018 at 6:12
  • @romainl it should, but actually it does not
    – Sang
    Commented Jun 29, 2018 at 6:54
  • did you add node_modules/ to your .gitignore?
    – gregory
    Commented Jul 21, 2018 at 3: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
    Commented Jul 21, 2018 at 14:40

2 Answers 2

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

2
  • 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 Commented Feb 19, 2019 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
    Commented Jul 12, 2020 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 ""'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.