5

I use the command locate --regex '/.git$' | grep username to get a list of all the Git repositories under my $HOME folder.

Is there a way to load them all into Projectile, to make them all instantly available?

8

You can add a single project to Projectile's runtime list of known projects (projectile-known-projects) using the command projectile-add-known-project, which interactively prompts you for the root directory of a project.

In order to add multiple projects at a time, you can use projectile-known-projects-file, which is read when projectile.el is first loaded. This file can either be modified by hand, or updated with the contents of projectile-known-projects using the command projectile-cleanup-known-projects. The contents of projectile-known-projects-file form a list of project root directories, e.g. ("~/src/emacs/" "~/src/projectile/").

ADDENDUM:

You could also write your own function which:

  1. calls a custom/external command (like the one you mention) resulting in a sequence of directory paths;
  2. adds the resulting directories to projectile-known-projects; and
  3. (optionally) writes projectile-known-projects to projectile-known-projects-file for persistence by calling (projectile-save-known-projects).

EDIT (tip for Git and Magit users):

If you use Magit, you can reuse some of its git repository locating functionality (see magit-repos.el) in your Projectile configuration. Here's an example from my own configuration:

  1. Configure magit-repository-directories (which see) to include the desired directories. Note that each entry can be associated with a subdirectory depth. If you organise all your projects as subdirectories of a select few parent directories, then only the parent directories need be added to magit-repository-directories, with the corresponding search depth. For example:

    (with-eval-after-load 'magit
      (setq magit-repository-directories
            '(;; Directory containing project root directories
              ("~/src/"      . 2)
              ;; Specific project root directory
              ("~/dotfiles/" . 1))))
    
  2. After Projectile is loaded, you can add all repositories reported by Magit to projectile-known-projects using something like:

    (with-eval-after-load 'projectile
      (when (require 'magit nil t)
        (mapc #'projectile-add-known-project
              (mapcar #'file-name-as-directory (magit-list-repos)))
        ;; Optionally write to persistent `projectile-known-projects-file'
        (projectile-save-known-projects)))
    

(If you use use-package you can replace with-eval-after-load 'projectile with use-package projectile :config.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.