How do I configure ctrlp to work correctly with ag outside of a git repo?
Asked Answered
H

2

9

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?

Hazelton answered 11/3, 2017 at 3:31 Comment(5)
Did you give the right link, in the 1st line of your question? I don't see anything related with itJoiner
Sorry about that -- link updated. Here's another similar vimrc with a slightly different ag command, which has the same behavior for me (i.e., doesn't work).Hazelton
Do you get 1 with :echo executable('ag')?Averir
Yes, :echo executable('ag') gives 1 as expected. Ag works perfectly both in Vim inside a git repo and from the command line.Hazelton
Why don't you ask on the plugin's issue tracker?Leucocytosis
H
3

PEBCAK. CtrlP and Ag are both doing what they are supposed to. I have a .git folder in my home directory, to back up dotfiles and a few other odds and ends. The .gitignore is set to ignore ~/Documents/, among other things. Therefore any time I run CtrlP from a subfolder of ~/Documents/, it finds no files (since it's inside of the home directory git repo but all files are ignored). This is why CtrlP is coming up blank; running it outside of ~/Documents/ works as expected.

However, I'm still unsure why the .git repo in the home directory doesn't screw up ag on the command line inside a subfolder of ~/Documents/; it only happens in Vim with CtrlP. (Again, I've confirmed that CtrlP is using the command ag %s -l --no-color -g "".) Any thoughts on that are welcome. I'm also not sure how to fix the issue except removing the git repo in my home directory (perhaps by hardlinking dotfiles in ~ into a git-protected dotfiles directory).

Hazelton answered 4/4, 2017 at 16:52 Comment(0)
A
0

ctrlp doesn't find any file if you're in outside of a Git repo with the setting you posted.

With the following setting, ctrlp uses git ls-files to retrieve file list if you're in a Git repo, otherwise ctrlp uses ag.

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

See :h g:ctrlp_user_command further details.

Averir answered 24/3, 2017 at 8:58 Comment(5)
I think my initial question may have been confusing. CtrlP works correctly when the working directory is a project folder that has a .git/ folder inside it, not just from inside the actual .git/ folder itself. In other words, if my project uses git, CtrlP works; if it doesn't, it doesn't. I edited the question to hopefully clarify this.Hazelton
What does :echo g:ctrlp_user_command say? I guess 'ag %s ...' thoughAverir
:echo g:ctrlp_user_command gives expected output: ag %s -l --no-color -g "" (in both the working and broken cases).Hazelton
hmm - that's weird. Have you tried the setting I posted above?Averir
Aha! Revelation. Your setting above led me to the cause of the issue (see answers). Thank you!Hazelton

© 2022 - 2024 — McMap. All rights reserved.