When I issue the command helm-projectile-switch-project, can I have it open the Most Recently Used file in that project, instead of asking me for a file?

If the file is already open, switch to that buffer?;)

Already a function for that, maybe?;)

1 Answer 1


I'm not sure such a command already exists, but here's one that I wrote a while ago:

(defun lw-switch-project ()
    (let* ((projects (projectile-relevant-known-projects))
           (project (f-expand (completing-read "Switch to project: " projects)))
           (windows (window-list))
           (opened-files (-filter #'buffer-file-name
                                  (projectile-project-buffers project)))
           (all-files (if (> (length windows) (length opened-files))
                           (--> (projectile-project-files project)
                                 (-separate (lambda (f) (or (f-ext-p f "py") (f-ext-p f "scala")
                                                            (f-ext-p f "sc") (f-ext-p f "el"))) it))
                                (-take (- (length windows) (length opened-files)) it)
                                (-map (-partial #'f-join project) it)
                                (-map #'find-file-noselect it)))
      (--each (-zip windows all-files)
        (set-window-buffer (car it) (cdr it)))))

It should prompt you for a project, then fill your windows with buffers belonging to that project, prioritising already open buffers and program files (simply checked by extension), then going by MRU. Though note it uses f.el and dash.el.

  • What do you mean with "fill your windows with buffers belonging to the project"?;) Will it try to open all of them?;) Also, I get this error when I try it: Debugger entered--Lisp error: (void-function prof-file-p). I could post the whole error, but I would need to filter it first, cause it contains some sensitive filenames;) Commented Feb 7, 2023 at 9:46
  • Ah sorry, I messed up whilst editing it, I've re-edited it to fix it (: Commented Feb 7, 2023 at 10:58
  • In terms of windows, I use workspaces each having its own window configuration, so it's useful for me to fill all windows in a given workspace when switching project with buffers from the new project. It should only open new files if there are no existing buffers belonging to the project, and then it should only open as many new files as you have windows. Probably invoking the command will give the best idea as to what it does (: Commented Feb 7, 2023 at 11:01
  • Yeah, this looks really awesome and seems to work excellently;) Thanks Commented Feb 7, 2023 at 11:06

Your Answer

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

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