I use 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 great if I'm running vim from a project folder with a .git
folder inside it. However, I get an empty file list (no results) whenever I run Vim from a directory that's not the root of a 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 ag
command, ag %s -l --nocolor -g ""
, works fine when I run it from the command line in the same directory (it finds all files).
Here's the entirety of my ctrlp
config:
map <c-p> :CtrlP<cr>
map <c-t> :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's 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")': ['<c-t>'],
\ 'AcceptSelection("t")': ['<cr>', '<2-LeftMouse>'],
\ 'ToggleType(1)': ['<c-u>', '<c-up>'],
\ 'ToggleType(-1)': ['<c-y>', '<c-down>'],
\ 'PrtExit()': ['<c-l>', '<esc>', '<c-c>', '<c-g>'],
\ 'PrtSelectMove("j")': ['<c-n>', '<down>'],
\ 'PrtSelectMove("k")': ['<c-p>', '<up>'],
\ 'PrtHistory(-1)': ['<c-j>'],
\ 'PrtHistory(1)': ['<c-k>'],
\ }
let g:ctrlp_buftag_types = {
\ 'coffee' : '--language-force=coffee --coffee-types=cmfvf'
\ }
How can I get ctrlp/ag
to work correctly outside of a git repo?
ag
command, which has the same behavior for me (i.e., doesn't work). – Hazelton1
with:echo executable('ag')
? – Averir:echo executable('ag')
gives1
as expected.Ag
works perfectly both in Vim inside a git repo and from the command line. – Hazelton