How to show files and contents of a directory in nvim-telescope if it is in .gitignore
The usecase is pretty simple here: I'm heavily using gitlab-ci job templates in various gitlab projects by using the include statement.
While this greatly reduces code duplication, it opens up for another problem: Many IDEs (neovim - well, telescope - among them) by default ignore files/directories present in .gitignore. This makes perfectly sense in most cases and is not somethin I'd like to generally switch off (even though it is easil possible).
But my idea here is different though: I'd like to provide the content of the gitlab-templastes project as a symlink in other projects to make the files searchable for telescope. At the same time I do not want to accidentially commit them to any project.
So, I do
ln -s /path/to/gitlab-templates
echo gitlab-templates >> .gitignore
So far so good, doing a git status does not show the symlink, but the files also do not show up in telescope (where I would like to make them searchable etc.)
The solution is pretty simple: In my case, telescope uses fzf and fd under the hood to search for files. Both support themselves the usage of the file ~/.ignore. This file can be used to provide a list of files and directories that should be ignored by the search. This does not yet help as we are trying to un-ignore files here, so now comes the trick:
- The .ignore files precedes over .gitignore
- The .ignore file can also contain negated rules.
Any rule that starts with an exclamation mark will explicitely unignore the pattern (same as in .gitignore files).
Thus, adding the following line to ~/.ignore will make the files under the symlinked directory gitlab-templates searchable in telescope, because they will not be ignored by fd:
cat ~/.ignore
!gitlab-templates
.git/
On the other hand, git will still ignore the files, so no need to worry about accidentially committing them.
Problem solved.