Use ag in ctrlp + vim
Asked Answered
B

1

26

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?

Billion answered 17/8, 2013 at 6:12 Comment(2)
Does my answer solve your problem? or do you need additional information?Vyner
It sure does! Forgot to mark your answer as accepted, sorry about that. Thank you very much!Billion
V
42

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.

Vyner answered 3/9, 2013 at 20:38 Comment(2)
from where did you know the "" part at the end of ag command for ctrlp?Coontie
The thing I always miss when setting up on a new computer is that you must place the .agignore in the home directory (i.e. ~/.agignore) not in the root of your application.Macadam

© 2022 - 2024 — McMap. All rights reserved.