如何配置ctrlp以便在git repo之外使用ag?

Sas*_*lla 7 git vim ctrlp ag

我用agctrlp,如建议在这里:

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
Run Code Online (Sandbox Code Playgroud)

如果我从包含文件夹的项目文件夹中运行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
Run Code Online (Sandbox Code Playgroud)

当我从同一目录中的命令行运行它时,实际的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'
\ }
Run Code Online (Sandbox Code Playgroud)

如何才能ctrlp/ag在git repo之外正常工作?

Sas*_*lla 3

PEBCAK。CtrlP并且Ag都在做他们应该做的事情。我的主目录中有一个.git文件夹,用于备份点文件和其他一些零碎的东西。除其他事项外,被.gitignore设置为忽略。~/Documents/因此,每当我CtrlP从 的子文件夹运行时~/Documents/,它都找不到文件(因为它位于主目录 git repo 内部,但所有文件都被忽略)。这就是为什么CtrlP显示为空白;按预期在工作之外运行它~/Documents/

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