Customize project detection

We may like to control whether a folder should be guessed as a project root. Sometimes you may see a folder annoyingly being added to project list, e.g., HOME, Dropbox root. This is mostly likely due to default set of a long list for projectile-project-root-files. For better control, it is good to reset it. You may find the policy in the well-written Projectile Docs.

1
2
3
4
  ;; control project root additional guess (besides ".git", ".projectile")
  (setq projectile-project-root-files
        '("CMakeLists.txt" "Makefile" "configure.ac" "configure.in"
          "TAGS" "GTAGS"))

Exclude user-specified projects/folders/files in Projectile

Excluding projects

To ignore folders that should never be listed as projects, simply set projectile-ignored-projects. For example,

1
2
  (setq projectile-ignored-projects '("~/"
                                      "~/Public/Dropbox/"))

However, in projectile-find-file or projectile-find-dir, it does NOT obey the rule. For example, it triggers annoying search of your home folder, which usually takes long time and freezes your Emacs. Moreover, you may not like certain folders or files to be listed when you use projectile to locate files or folders.

Since projetile-indexing-method is default set to alien, both projectile-globally-ignored-directories and projectile-globally-ignored-files will not be used. However, choosing native indexing method compromises speed performance considerably, especially for large projects.

Suppose we prefer alien for best performance, there is a way to get both satisfied. The idea is to set projectile-generic-command that is used for alien type indexing. Assume we use ripgrep (terminal command rg), we can add arguments to rg to ignore folders/files as you prefer,

  • ignoring folder by option --glob with folders read from projectile-globally-ignored-directories that you set;

  • ignoring files by option --ignore-file. However, this option specifies a file that lists all files to be ignored. The easy way is to create a global file (e.g., in ~/.emacs.d/) and list there all files that you like to put to projectile-globally-ignored-files. For example, we set a file name ~/.emacs.d/rg_ignore.

The whole setup as an example is given below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
  ;; Due to "alien" indexing method, globally ignore folders/files by
  ;; re-defining "rg" args
  (mapc (lambda (item)
          (add-to-list 'projectile-globally-ignored-directories item))
        '("Backup" "backup" "auto" "archived"))
  ;; files to be ignored should be listed in "~/.emacs.d/rg_ignore"

  ;; Use the faster searcher to handle project files: ripgrep "rg"
  (when (and (not (executable-find "fd"))
             (executable-find "rg"))
    (setq projectile-generic-command
          (let ((rg-cmd ""))
            (dolist (dir projectile-globally-ignored-directories)
              (setq rg-cmd (format "%s --glob '!%s'" rg-cmd dir)))
            (setq rg-ignorefile
                  (concat "--ignore-file" " "
                          (expand-file-name "rg_ignore" user-emacs-directory)))
            (concat "rg -0 --files --color=never --hidden" rg-cmd " " rg-ignorefile))))