Emacs Redux: Smarter Minibuffer Prompts in Projectile

Wait 5 sec.

Projectile asks you a lot of questions.What do you want to search for? Replace it with what? Which command should Irun? For as long as I can remember those prompts were about as dumb as a promptcan be - an empty line, maybe with the symbol at point as the default, andyou had to type the rest.That’s a bit silly when you think about it, because by the time Projectile asksyou something it already knows quite a lot - the project you’re in, the functionyour cursor is sitting in, the Makefile at the root of the project and what’sin it. I’ve been meaning to do something about this for ages, and in theupcoming Projectile 3.5 a bunch of prompts finally start to use that knowledge.Let’s go over them!The Search PromptEvery command that asks what to search for (projectile-search,projectile-grep, projectile-ripgrep, the search reviewer,projectile-find-references and friends) now uses the same prompt. It’s still aplain text prompt, so whatever you type is what gets searched for, but it haslearned a few new tricks: RET on an empty prompt searches for the symbol at point (or the region), as before M-n goes through a few more guesses - the name of the function you’re in,the name of the current file and the last thing you looked for with Isearch M-p goes through your previous searches, and the search prompts now have ahistory of their own TAB completes the word before point against the identifiers that occur in the project (super handy)Here’s all of this in action:The TAB completion is my favorite part. I can never remember whether somethingwas named projectile-replace-review or projectile-review-replace, and now Idon’t have to! The candidates are sorted by how often they show up in theproject, so the names you actually use end up at the top.Where do the candidates come from? There’s no index and no tags file involved.Projectile just runs rg (or git grep, if you don’t have ripgrep installed)for the prefix you’ve typed and collects the words that start with it. Thattakes a few milliseconds on a small project and a fraction of a second onsomething as big as Emacs itself. Not too shabby, right? You need to type atleast two characters for the completion to kick in. And if you don’t like it,you can always turn it off:(setq projectile-search-complete-identifiers nil)Note: You might be wondering why the prompt isn’t simply usingcompleting-read. That would have been the easiest way to get completion, butwith something like Vertico pressing RET would then select the currentcandidate instead of what you’ve typed, and I really don’t want a search promptthat second-guesses my input. That’s why it’s a plain read-from-minibuffer with acompletion-at-point function installed in it, so TAB works the same way itdoes in a code buffer. In the screencast above the candidates are displayed byconsult-completion-in-region. Without it you’ll get the stock *Completions*buffer.The Replace Promptsprojectile-replace reads what to replace with the same prompt, so everythingabove applies there as well. When it asks what to replace it with, M-nbrings up the original text. After all, most replacements are a small edit ofthe thing they replace (e.g. a rename), so it’s nice not to have to type itagain.With a prefix argument the search and replace commands ask which files to workon, and those prompts now complete over the file extensions that are actuallypresent in the project. (the most common ones come first)The Command PromptsThe compile, test and run prompts got the same treatment. M-p has always gonethrough the commands you’ve run before. Now M-n goes the other way - throughcommands you haven’t run yet, but the project suggests - the default command forthe project’s type, followed by the commands of itstasks (Make targets, npmscripts, just recipes and so on). The tasks named after what you’re doing comefirst, so the test prompt starts with things like make test:The same suggestions show up on M-n when you run a shell command in theproject root with s-p ! or s-p &.Filtering Search ResultsProjectile’s search and replace results buffers have had filters for a while -k/d to keep or flush matches by line, and K/D to do the same by file.The file filters expect a regular expression and most of the time people typepretty much the same thing there - something like ^test/.Now the prompt suggests it for you. The default is the directory of the match at point,and M-n goes on to that file’s extension, the file itself and then thetop-level directories that have matches, busiest first. Getting rid of all thematches in the tests is D RET:Odds and Ends C-u s-p C (projectile-find-changed-file with a prefix argument) asks for arevision to compare against. It now defaults to the project’s main branch,whatever it happens to be called, and it completes over tags as well, so“what changed since the last release” is a M-n or two away. projectile-add-known-project starts at the root of the project you’re in,instead of whatever subdirectory the current buffer lives in.M-n is UnderratedYou’ve probably noticed that M-n shows up in pretty much every section ofthis article. Every Emacs user knows that M-p in the minibuffer goes backthrough the history. I’m not sure how many people know that M-n goesforward, into what the Emacs manual calls “future history” - a list ofguesses that the command has made about what you might want to type. A lot ofbuilt-in commands provide those (try M-n at the query-replace or find-fileprompts), but you’ll never see them unless you ask, so they are easy to miss.It’s one of those fairly obscure Emacs features that really deserve to bebetter known.For package authors that’s probably the cheapest way to make a prompt smarter.The functions that read from the minibuffer (read-string,read-from-minibuffer, completing-read, read-shell-command) all accept alist as their default value, and that list is what M-n goes through:(read-string "Search for: " nil 'my-search-history (delq nil (list (thing-at-point 'symbol t) (add-log-current-defun) (and buffer-file-name (file-name-base buffer-file-name)))))It doesn’t get in anyone’s way, it works with every completion framework, andit’s only a few lines of code. I plan to write a bit more about M-n soon, asit certainly deserves an article of its own.Closing ThoughtsNone of those changes is a big deal on its own, but together they makeProjectile feel a lot smarter to me. All of this will be a part of Projectile3.5, and most of it is already available in the master branch if you want toplay with it today. If you can think of other prompts that could do a better jobof guessing what you want - I’d love to hear about them!That’s all I have for you today. Keep hacking!