如何配置 ctrlp 以在 git 存储库之外与 ag 一起正常工作?

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

我按照建议将 agctrlp 一起使用 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

如果我 运行ning vim 来自一个包含 .git 文件夹的项目文件夹,这非常有效。但是,每当我从非 git 项目的根目录中 运行 Vim 时,我都会得到一个空文件列表(无结果)。澄清一下,使用以下文件夹层次结构:

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

实际的 ag 命令 ag %s -l --nocolor -g "" 当我从同一目录中的命令行 运行 它时工作正常(它找到所有文件)。

这是我的全部 ctrlp 配置:

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'
\ }

如何让 ctrlp/ag 在 git 存储库之外正常工作?

如果您在具有您发布的设置的 Git 存储库之外,ctrlp 找不到任何文件。

使用以下设置,如果您在 Git 存储库中,ctrlp 使用 git ls-files 检索文件列表,否则 ctrlp 使用 ag.

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

查看 :h g:ctrlp_user_command 更多详细信息。

PEBCAK。 CtrlPAg 都在做他们应该做的事情。我的主目录中有一个 .git 文件夹,用于备份点文件和其他一些杂七杂八的东西。 .gitignore 设置为忽略 ~/Documents/,等等。因此,每当我从 ~/Documents/ 的子文件夹 运行 CtrlP 中找到任何文件时(因为它位于主目录 git 存储库中,但所有文件都被忽略)。这就是 CtrlP 空白的原因; 运行在 ~/Documents/ 之外使用它按预期工作。

但是,我仍然不确定为什么主目录中的 .git 存储库不会在 ~/Documents/ 的子文件夹内的命令行上搞砸 ag;它只发生在 Vim 和 CtrlP 中。 (我再次确认 CtrlP 正在使用命令 ag %s -l --no-color -g ""。)欢迎提出任何想法。我也不确定如何解决这个问题,除了删除我主目录中的 git 存储库(可能通过将 ~ 中的点文件硬链接到 git-protected dotfiles 目录).