Use ag in ctrlp + vim

13.6k views Asked by At

I want to use ag (silver searcher) with ctrlp and vim. I have this in my .vimrc:

if executable("ag")
    set grepprg=ag\ --nogroup\ --nocolor
    let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
endif

let g:ctrlp_show_hidden = 1

set wildignore+=*/tmp/*,*.so,*.swp,*.zip,*/.tmp/*,*/.sass-cache/*,*/node_modules/*,*.keep,*.DS_Store,*/.git/*

I want ctrlp to include hidden files but those are hidden. If I add -u to the ag command it shows all hidden files but doesn't respect the wildignore or .gitignore. Is it possible to make it respect these?

1

There are 1 answers

2
mcanfield On BEST ANSWER

If you are using a custom finder via ctrlp_user_command several options, including ctrlp_show_hidden ctrlp_custom_ignore and vim's wildignore patterns, are not used by CtrlP (see documentation).

g:ctrlp_show_hidden

...

Note: does not apply when a command defined with |g:ctrlp_user_command| is being used.

g:ctrlp_custom_ignore

...

Note #1: by default, |wildignore| and |g:ctrlp_custom_ignore| only apply when |globpath()| is used to scan for files, thus these options do not apply when a command defined with |g:ctrlp_user_command| is being used.

So you are left at the mercy of your searching tool, in this case, ag. Fortunately you can do a couple things that should give you the behavior you want.

To get your hidden dotfiles to appear, but still respect ignore files, use the --hidden option for ag:

let g:ctrlp_user_command = 'ag %s -l --nocolor --hidden -g ""'

Now for defining patterns to ignore, you can use ag's own ignore file .agignore. This can be a per directory or a global one that ag will check on each run. You place that in your home dir ~/.agignore.

I understand it can be nice to have vims wildignore take care of patterns, but with .agignore you get the bonus of those restrictions when using ag from the cli. If you want to search all files just use the ag -u command you mentioned to bypass any ignore files.

As a final tidbit, there is a Dictionary format you can use to define g:ctrlp_user_command which contains an ignore key that will make CtrlP use wildignore patterns. However, I've never tried this and the documentation points out a potential performance hit. You might try this method if you don't like my other proposed solution (see documentation).

Note #3: unless the |Dictionary| format is used and 'ignore' is defined and set to 1, the |wildignore| and |g:ctrlp_custom_ignore| options do not apply when these custom commands are being used. When not present, 'ignore' is set to 0 by default to retain the performance advantage of using external commands.