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?

1 Answer 1

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!

3
  • 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, 2020 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, 2020 at 11:44
  • Any ideas how can we implement this with counsel-projectile? Jul 12, 2020 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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