How to enable AG in vim + fzf

  1. By default, fzf uses the system's "find" command. You can change that by altering the env variable $FZF_DEFAULT_COMMAND to a third-party utility. The advantage to using these tools is that they observe ignore files. Using silver searcher (i.e. ag), you're default command would look something like the following:

    $echo $FZF_DEFAULT_COMMAND ag -l --path-to-ignore ~/.ignore --nocolor --hidden -g ""

    Regardless of the FZF command used, at the shell you're gathering up file names that match your criteria.

  2. FZF+Vim, an extra plugin in addition to fzf, provides an :Ag command to use in (g)vim. The :Ag command lets you search the contents of files, opposed to its :Files command, which only searches file names.

    There is some .vimrc customization one might want when using FZF+Vim. For example, the tweak I like is the option to run :Ag with a full screen search and preview window (use :Ag! instead of :Ag):

    command! -bang -nargs=* Ag \ call fzf#vim#ag(<q-args>, \ <bang>0 ? fzf#vim#with_preview('up:60%') \ : fzf#vim#with_preview('right:50%:hidden', '?'), \ <bang>0)


To have a preview window for the file under cursor during search, you can add below to the .vimrc or init.vim

command! -bang -nargs=* Ag
  \ call fzf#vim#grep(
  \   'ag --column --numbers --noheading --color --smart-case '.shellescape(<q-args>), 1,
  \   fzf#vim#with_preview(), <bang>0)

Then use :Ag mySearchTerm to see the result with a preview on the right side.

You can also map ctrl-g to it for convenience:

map <C-g> :Ag


You need to install ag, in case you are on a mac:

brew install the_silver_searcher

As another option for testing fzf you could also use ctrl p to call :Files by using this map:

nnoremap <c-p> :Files<CR>

And you could use ripgrep when calling :Files, for this you will need to modify the default FZF_DEFAULT_COMMAND:

export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow --glob "!.git/*"'

You can find more information here: https://github.com/junegunn/fzf#usage

Tags:

Vim

Ag

Fzf