How to enable AG in vim + fzf
Asked Answered
A

3

19

I am new to vim. I read online that one of the best fuzzy finder in vim is fzf. I have started using it. But whenever I write the command :Ag I get ag is not found. I don't know what it means and I don't know how to fix it.

Arielariela answered 1/3, 2018 at 15:0 Comment(0)
Y
20

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

Youthful answered 1/3, 2018 at 15:23 Comment(0)
S
14
  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)

Stereochemistry answered 6/6, 2018 at 22:22 Comment(0)
D
7

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

Doubly answered 10/1, 2020 at 22:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.