Search code examples
gitvimgitignorectrlp

CtrlP: ignore files in subdirectory within a git repo


For my repo, suppose it's called top

top\
  .gitignore
  foo\
  bar\

I want CtrlP to list files that are

  1. Under foo\
  2. Is not ignored by .gitignore defined in top\

If I cd into top\foo\, and open vim from there, 1 would be satisfied but not 2.
If I open vim on top\, 2 would be satisfied but not 1.

How do I achieve both 1 and 2?

I tried this gitignore vim script, but it only parses gitignore when I open vim in the root folder of a repo, so I can't do both 1 and 2 together.
Same for let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""', ag doesn't try to go up to the repo root and read gitignore from there.
Setting g:ctrlp_working_path_mode as r let CtrlP honors .gitignore in the top folder, but everything under top\ would be listed by CtrlP. I just want files in foo\.

Thanks.


Solution

  • From its home page it looks like you can use

    let g:ctrlp_user_command = [
        \ '.git', 'cd %s && git ls-files . -co --exclude-standard',
        \ 'find %s -type f'
        \ ]
    

    which will have the advantage of honoring all of git's ignore-pattern processing, not just the toplevel positive .gitiginore patterns.