2

I'm using helm-projectile in spacemacs.

SPCpp runs helm-projectile-switch-project, which gives me a helm completion buffer for projectile projects.

When I choose a project and hit enter, I then get a completion buffer to choose which file I want to open in that project.

I'd like to skip that second step, and just have a default file associated with a project that always opens first when I open that project. Is that possible?

ngm
  • 291
  • 1
  • 6

1 Answers1

1

There is no official support for that in projectile (I think) but, you can use dir-local-variables and a little bit of hacking code like this:

(defun open-local-file-projectile (directory)
  "Helm action function, open projectile file within DIRECTORY
specify by the keyword projectile-default-file define in
`dir-locals-file'"
  (let ((default-file (f-join directory (nth 1
                         (car (-tree-map (lambda (node) 
                                   (when (eq (car node) 'projectile-default-file)
                                 (format "%s" (cdr node))))
                                 (dir-locals-get-class-variables (dir-locals-read-from-dir directory))))))))
    (if (f-exists? default-file)
    (find-file default-file)
      (message "The file %s doesn't exist in the select project" default-file)
      )
    )
  )


(add-to-list 'helm-source-projectile-projects-actions '("Open default file" . open-local-file-projectile) t)

This code search for the file defined in dir-locals-file (by default ".dir-locals.el") and search for the key: projectile-default-file, that should be the file route, without the projectile root, then we concat the path of the project root with the default-file relative route, and check if the default-file exist, if it exist, we open it as desired.

We can trigger the "Open default file" action by pressing the tab key in the helm menu.

This code requires the libraries dash and f to work.

Feel free to ask any question about this messy code, good luck!

Fermin MF
  • 635
  • 3
  • 8
  • Thanks! I'm trying this and when I press tab in the helm-project menu I am currently getting the message "Aborting an helm session running in background". – ngm Jul 11 '20 at 10:19
  • The add-to-list function doesn't seem to add the action for me - I've used (helm-add-action-to-source) instead. – ngm Jul 11 '20 at 11:44
  • Any ideas how can we implement this with `counsel-projectile`? – Swarnendu Biswas Jul 12 '20 at 08:34