How to get fzf.vim ignore node_modules and .git folders?

Viewed 19393

I have fzf setup in vim with barely any customisation:

" fzf and ripgrep settings
set rtp+=/usr/local/opt/fzf
let g:fzf_action = {
    \ 'ctrl-t': 'tab split',
    \ 'ctrl-i': 'split',
    \ 'ctrl-s': 'vsplit'
    \ }

In my .bash_profile, I set the environment variable FZF_DEFAULT_COMMAND to show hidden, but ignore node_modules and .git:

export FZF_DEFAULT_COMMAND='rg --files --follow --no-ignore-vcs --hidden -g "!{node_modules/*,.git/*}"'

However, when I use :Files function in vim, it still searches in the folders i want ignored.

6 Answers

Have you tried :GFiles command?

It is one of the best FZF command that is excluding anything that is in .gitignore.

Personally I like to use it as default.

:help fzf-vim-commands

Silly me. I forgot to source ~/.bash_profile

You can ignore the node_modules (and any other folder you want to ignore) by using a custom FZF_DEFAULT_COMMAND. I use something like this:

(place it in your vimrc)

let $FZF_DEFAULT_COMMAND='find . \( -name node_modules -o -name .git \) -prune -o -print'

I suppose 'junegunn/fzf' are we tolking about.
I have success on make fzf and rg work for my need. (I'm a developer)
this is my vim 8.2 configuration:

command! -bang -nargs=* Rg
  \ call fzf#vim#grep(
  \   "rg -g '!design/' -g '!dist/' -g '!pnpm-lock.yaml' -g '!.git' -g '!node_modules' --column --line-number --no-heading --color=always --smart-case ".shellescape(<q-args>), 1,
  \   fzf#vim#with_preview({'options': '--exact --delimiter : --nth 4..'}), <bang>0)

In this configuration inside last row --exact remove all fuzzy search: how developer I like exatc metch more

to not include folder and file I do not like I use -g option:

rg -g '!design/' -g '!dist/' -g '!pnpm-lock.yaml' -g '!.git' -g '!node_modules'

The ! is to exclude
I'm excluding design, dist folder from my search base path (current directory)
and exclude il file pnpm-lock.yaml anywhare
and excluse .git and node_modules anywhare (file or folder)

hope can help, regards, Leonardo

You can use fzf.vim

It allows you to use default $FZF_DEFAULT_COMMAND if defined.

If you are using vim-plug for plugin management, to install fzf.vim:

Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'

In case it results in:

command failed: rg --files ...

It is because it results from an rg error or you do not have it installed, for that you just have to execute

sudo apt install ripgrep -y

In case you use a script for the installation you can use:

which rg >/dev/null 2>&1 ||
    apt install ripgrep -y
Related