Can I get the Vim Ctrlp plugin to ignore a specific folder in one project?
Use a custom listing command
Ctrlp lets you tell it what command to use to get a list of files in the folder. So if you wanted to exclude anything named beets.txt
, you could do:
let g:ctrlp_user_command = 'find %s -type f | grep -v "beets.txt"'
That's global, but it starts to point toward the answer: supply your own shell command.
Even better, Ctrlp lets you supply multiple shell commands with markers, meaning "if you see this marker in the root directory, use this command."
I found this in :help ctrlp
, and modified slightly based on the author's comment on an issue.
let g:ctrlp_user_command = {
\ 'types': {
\ 1: ['.git', 'cd %s && git ls-files --cached --exclude-standard --others'],
\ 2: ['.hg', 'hg --cwd %s locate -I .'],
\ },
\ 'fallback': 'find %s -type f'
\ }
This means: "If you see .git
in the folder, use git ls-files...
. Otherwise, if you see .hg
, use hg --cwd...
, otherwise use a regular find
."
So, to ignore a specific folder in one project, devise a command that will ignore that folder, then place a unique marker in that project to let Ctrlp that you want to use your special command here.
(In my case, I actually wanted to ignore files that were in .gitignore
, so the git ls-files
command above works for me.)
If you are using the Silver Searcher backend for CtrlP (which is far faster), just add an .agignore
file to your project directory in the same format as a .gitignore
:
.git/
.hg/
.svn/
Alternatively, keep a global ~/.agignore
file.
Add the Silver Searcher as the backend with this in your .vimrc
let g:ctrlp_user_command = 'ag %s -l --nocolor --hidden -g ""'
Specifies intentionally untracked files in a file
To solve this with a file like .gitignore
(based in the Nathan grep solution), I created a file named .ctrlpignore
and put the patterns that should be ignored, separated by lines:
node_modules/
\.log$
...
And my ctrlp configuration:
let g:ctrlp_user_command = 'find %s -type f | grep -v "`cat .ctrlpignore`"'
Maybe the .gitignore
itself can be used to ignore the files in ctrlp, not needing to create a new file to do almost the same thing.