Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

vim

ag

ctrlp

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?

like image 564
Sasgorilla Avatar asked Mar 11 '17 03:03

Sasgorilla


1 Answers

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).

like image 57
Sasgorilla Avatar answered Nov 14 '22 03:11

Sasgorilla