How to configure ctrlp to work properly with ag outside the git repository?

I am using ag with ctrlp as suggested here :

 if executable('ag') set grepprg=ag\ --nogroup\ --nocolor let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""' let g:ctrlp_use_caching = 0 else let g:ctrlp_custom_ignore = '\.git$\|\.hg$\|\.svn$' let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files . --cached --exclude-standard --others'] endif 

This works fine if I run vim from a project folder with a .git folder inside it. However, I get an empty list of files (with no results) when I start Vim from a directory that is not the root of the git project. To clarify, with the following folder hierarchy:

 Proj1/ # ctrlp works; finds foo.js and bar.js (unless they are .gitignored) .git/ bin/ foo.js bar.js Proj2/ # ctrlp doesn't work; has empty file list bin/ foo.py bar.py 

The actual command ag ag %s -l --nocolor -g "" works fine when I run it from the command line in the same directory (it finds all the files).

Here is all my ctrlp config:

 map <cp> :CtrlP<cr> map <ct> :CtrlPTag<cr> let g:ctrlp_dotfiles = 1 let g:ctrlp_show_hidden = 1 let g:ctrlp_cmd = 'CtrlPMixed' " search anything (in files, buffers and MRU files at the same time.) let g:ctrlp_cmd = 'CtrlP' let g:ctrlp_working_path_mode = 'ra' " search for nearest ancestor like .git, .hg, and the directory of the current file let g:ctrlp_match_window = 'top,order:ttb' let g:ctrlp_max_height = 12 " maxiumum height of match window let g:ctrlp_switch_buffer = 'et' " jump to a file if it open already let g:ctrlp_use_caching = 1 " enable caching let g:ctrlp_clear_cache_on_exit = 0 " speed up by not removing clearing cache evertime let g:ctrlp_mruf_max = 250 " number of recently opened files if exists('g:ctrlp_user_command') unlet g:ctrlp_user_command end if exists('g:ctrlp_custom_ignore') unlet g:ctrlp_custom_ignore end if executable('ag') set grepprg=ag\ --nogroup\ --nocolor let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'" let g:ctrlp_use_caching = 0 else let g:ctrlp_custom_ignore = '\.git$\|\.hg$\|\.svn$' let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files . --cached --exclude-standard --others'] endif let g:ctrlp_prompt_mappings = { \ 'AcceptSelection("e")': ['<ct>'], \ 'AcceptSelection("t")': ['<cr>', '<2-LeftMouse>'], \ 'ToggleType(1)': ['<cu>', '<c-up>'], \ 'ToggleType(-1)': ['<cy>', '<c-down>'], \ 'PrtExit()': ['<cl>', '<esc>', '<cc>', '<cg>'], \ 'PrtSelectMove("j")': ['<cn>', '<down>'], \ 'PrtSelectMove("k")': ['<cp>', '<up>'], \ 'PrtHistory(-1)': ['<cj>'], \ 'PrtHistory(1)': ['<ck>'], \ } let g:ctrlp_buftag_types = { \ 'coffee' : '--language-force=coffee --coffee-types=cmfvf' \ } 

How can I get ctrlp/ag to work correctly outside of git repo?

+7
git vim ag ctrlp
source share
2 answers

PEBCAK. CtrlP and Ag do what they have to do. I have a .git folder in my home directory for backing up dotfiles and several other options. .gitignore set to ignore ~/Documents/ , by the way. Therefore, at any time when I run CtrlP from the ~/Documents/ subfolder, it does not find files (since it is inside the git repo source directory, but all files are ignored). This is why CtrlP becomes empty; running it outside ~/Documents/ works as expected.

However, I'm still not sure why the .git repository in the home directory will not mess Ag on the command line inside the ~/Documents/ subfolder; this only happens in Vim with CtrlP . (Again, I confirmed that CtrlP uses the command ag %s -l --no-color -g "" .) Any thoughts on this are welcome. I also don’t know how to fix the problem besides deleting the git repository in my home directory (possibly by binding the dotfiles to ~ to the git -protected dotfiles ).

+3
source share

ctrlp does not find the file if you are outside the Git repository with the parameter you specified.

The next time you configure ctrlp uses git ls-files to retrieve a list of files if you are in the Git repository, otherwise ctrlp uses ag .

 let g:ctrlp_user_command = { \ 'types': { \ 1: ['.git', 'cd %s && git ls-files'] \ }, \ 'fallback': 'ag %s -l --nocolor -g ""' \ } 

See :hg:ctrlp_user_command .

0
source share

All Articles