reddit / emacs
Projectile: how to
CharacterBeginning94 · 2022-05-11 12:54
Is there a decent tutorial on Projectile? I can't find one.
In the short, how do I open a project? I know I can open a file and then projectile will (after bugging me with loads of stupid questions, apparently) open the project that I had open yesterday that contains the file in question, but that's completely backwards if you ask me. I don't need Projectile if I already know what file I want. What I want to do is say "Open project X", and then from there navigate to the files I'm interested in.
Alternatives welcome, if you have one that you prefer, because so far projectile seems to be wasting my time.
TheFrenchPoulp · 2022-05-11 13:44
I don't quite understand the difference between your desired behavior vs. current
My use workflow is usually one of:
1. `gp`, select the project, `g` to open the Magit buffer
1. Same but `s` to grep for something instead
1. Or `f` if I already know what file I'm after
1. `gb` to switch to an existing buffer if I already did 1, 2 or 3 in that session (or `B` to open in a new window)
Typically, 1 is for when I go on a new task and am just looking to create a new branch, 2 is when I'm looking for a specific bit of code but outside of the project I'm currently working on. 3 is for when I'm looking for that constant value in another project that I can't seem to remember no matter how many times go and look at it.
Given the very unwise uptime of my Emacs sessions on my machines though, 99% of the time I just use `gb` and `gB`
Not that it matters but I'm using project.el (built-in) and the workflow that I describe above: select a project then prompted for an action exists in both Projectile and project.el (and can be disabled in both)
For reference here are the actions that I have (re)defined:
((project-dired "Root" "D")
(project-find-file "File" "f")
(magit-project-status "Git" "g")
(me/project-search "Search" "s")
(me/vterm-dwim "Terminal" "t"))
CharacterBeginning94 · 2022-05-11 14:15
> I don't quite understand the difference between your desired behavior vs. current
You don't understand the difference between having to remember exactly where some file is (and having to type the path to it), and remembering the name of a project?
genehack · 2022-05-11 14:37
Does `M-x projectile-switch-project` do what you're looking for? (Sorry, I don't think there's a tutorial beyond the Projectile manual; IIRC it is pretty short and worth a scan.)
TheFrenchPoulp · 2022-05-11 15:18
If I search for `SomeFeature.test.ts` in the project Foo on my file system I'll end up typing something like `feat test` and since there will be too many results I'll complete the query ie. `feat test foo`.
If I first *move* to the Foo project, I'll search projects by name, then query files with `feat test` still so no I don't understand the difference in what you're asking :-)
Project/ile provides commands to find and/or act on things within project contexts, if you want better filtering ie. not having to type out exact paths I'd look into the `completion-styles` variable: I use the excellent [`orderless`](https://github.com/oantolin/orderless) myself
flylikeabanana · 2022-05-11 15:26
I just `projectile-switch-project` to the build file for my project, which is usually in the project root with a pretty unchanging path name, and navigate from there. Thats what I do e.g. when I just want to perform some git actions in a project without needing to visit a specific file.
nullmove · 2022-05-11 15:37
> What I want to do is say "Open project X", and then from there navigate to the files I'm interested in' description.
I don't think your characterisation of projectile is correct, because you need to use `projectile-switch-project` as entry function (as the other comment noted) which does ask you for the project first as you want, and only then action (which could be find file in project).
In any case, you might want to look at built-in `project.el` as alternative. Using it is also likewise easy. Just hit `C-x p p` (`project-switch-project`) and then choose the directory for the project (note that by default its notion of a project is a versioned controlled root directory, but you can plug new logic with some elisp if you need something else). There should now be a menu asking what you want to do in the project, e.g. pressing "f" will run `project-find-file` which is what you want. You can discover what more you can do with `C-x p C-h` for example.
Vultan · 2022-05-11 17:42
I'm totally with you, and I had the same experience when I got started with projectile. After trying a number of different variations, I've been really happy with this setting, which I put in my startup file:
`(setq projectile-switch-project-action #'projectile-dired)`
I can then use `M-x projectile-switch-project`, pick the project I want, and then it lands me in a dired buffer for the project. This works great as a "home page" for the project. I can then navigate within dired to open a file, or use some other command.
The other problem you may be having is that `projectile-switch-project` won't show your project in the list of projects if you haven't already opened up a file there once before. But once you've done it once, then projectile will remember it. You can preempt that problem if you run `projectile-discover-projects-in-directory`, which works reasonably if you have a directory where many of your projects are.
Finally, I'll add that there are a number of great YouTube videos on projectile. Just search for emacs and projectile and you'll find them. They helped me a lot in getting started. The one from [Mike Zemansky](https://www.youtube.com/watch?v=wBfZzaff77g&t=496s) is pretty good in particular. I'm a Doom Emacs user, and so I also really liked this [video on using projectile in Doom](https://www.youtube.com/watch?v=Rx3wRl5d-J0). I'd still recommend it even if you're not using Doom Emacs, just be aware that the keyboard shortcuts might be wrong.
xtifr · 2022-05-11 18:09
I usually just open the project directory (which is what I used to do anyway, *before* projectile), and it detects the version control system in use and uses that to define and activate the project. The whole thing seems to be *entirely* automatic.
If you're not using a version control system, well...first, why on earth not? What sort of crazed masochist are you? :) And second, I believe you can put some sort of special file in the project's base directory, and it will use that as the equivalent of the version control info (but without the version control). I can't offer any details, since I haven't had a project without version control for *many* years*.* But I'm pretty sure it *is* explained in the docs.
CharacterBeginning94 · 2022-05-12 06:06
Thanks, I'll give all this a go.
Edit: I tried those things but they didn't work. There are still "no known projects" according to -switch-project, even after running projectile-discover-projects-in-directory.
CharacterBeginning94 · 2022-05-12 06:10
It would do, if I had a project open, but until I do then it says "There are no known projects". That might be different if it took any notice of the contents of the projectile-known-projects-file, but it doesn't.
CharacterBeginning94 · 2022-05-12 06:12
You can only do those things if you already have files open. My entire point is that I do not.
CharacterBeginning94 · 2022-05-12 06:13
If I have to find a project directory to then find and open files... what would I need projectile for?
TheFrenchPoulp · 2022-05-12 09:37
What are you referring to? You don't need any file open to grep for code in a project, nor filter by file name
CharacterBeginning94 · 2022-05-12 10:01
So you're saying I shouldn't be using emacs to search for a file in order to start a project? Right.
What is easier/quicker, opening emacs and typing something like "projectile-open-project Foo" which then gives easy access to all the files in the project, or using who knows what tool, command line or GUI, composing a suitable search term to search for a file then opening it either by "emacs foo.c" on the command line or clicking? You, apparently, like to have to think about where your files are and to type a lot. I have better things to do.
Vultan · 2022-05-12 10:24
How about after you just open a file (directly) within a project? Does that project then appear as a known project to switch to?
TheFrenchPoulp · 2022-05-12 10:56
Everything I suggested is done within Emacs... I'll repeat myself (again) with simpler words perhaps:
1. Select your project
1. Select the action eg. find a file, find code, open a terminal, ...
1. Filter candidates (optional)
To me the only thing faster than this is to directly switch to a buffer when one already exists
CharacterBeginning94 · 2022-05-12 12:49
Do you ever actually read what has been written? There are no projects open. This entire thread is about how to open a project.
JFC.
CharacterBeginning94 · 2022-05-12 12:50
I have no desire to "just open a file". That's the whole point.
Vultan · 2022-05-12 13:00
Sure, that makes complete sense. I was just suggesting this as a troubleshooting technique to help me to help you diagnose what's going wrong with the discover process. I'm happy to just leave it be if you would prefer.
TheFrenchPoulp · 2022-05-12 13:38
I do. Do you? Opening a project is my step 1
Edit: I feel like I'm not using the right words or you're not reading them right. If you have an actual use case in mind, I can make a quick screencast of it
https://drive.google.com/file/d/1GH0ntCJ5JHmjdmOAqLlos0MsWRA2IyIa/view
LuisHGH · 2022-05-12 15:42
I think there are two easy ways to achieve what I think you're actually asking:
1 - including in your init.el something like `(setq projectile-project-search-path '("~/projects/" "~/work/" ("~/github" . 1)))`
2 - executing `M-x projectile-add-known-project` when you want to work with a new project
xtifr · 2022-05-12 19:16
To get completion on any files in the project without hunting through subdirs.
To allow the grep and compile commands to work from anywhere in the project, rather than needing to navigate back to the project root.
To avoid being confused by generated files--I don't have to remember that I need to edit config.h.in instead of config.h, because projectile-find-file doesn't even offer me config.h.
There's more, but those are the main reasons I keep it around. Yes, it's only a minor convenience, but it's still a convenience. Also, it's most useful if you also use an enhanced completion framework like ido or ivy, but why wouldn't you?
credmp · 2022-05-13 12:12
Projectile doesn't know about your project until you actually open it, or let it be discovered as many have already posted.
When you think of "Open Project X", in other editors it generally means that you will open a folder into a tree structure, from there you open a file of your choosing. Basically when you just visit a file or directory Projectile will recognize it as such and you will have all the project management tools at your fingertips. Is this what you expect from this functionality? In essence a "Open Folder-which-happens-to-be-a-project" function?
AFAIK there is no program that just "knows" about projects on your machine without it either scanning for it or you telling it about it.
Cheers