Posts

  • Theme Families and theme-choose-variant

    A while ago I wrote that multi-variant themes have become the norm - pretty much every popular theme these days is a family of a few dark variants and a light one. And every such family ships its own command for moving between the variants: modus-themes-toggle, ef-themes-select, tokyo-night-select, batppuccin-select and so on.

    I have to admit that I had completely missed that Emacs has a built-in command for this since Emacs 29 - theme-choose-variant. I learned about its existence only recently, when I tried it with my own Tokyo Night theme and got this in return:

    Theme `tokyo-night' does not have any known variants
    

    Not cool, given that the theme has four variants! So, what’s going on here?

    Read More
  • M-n: The Minibuffer's Future History

    Every Emacs user knows that pressing M-p in the minibuffer goes back through the history of the things they’ve typed there before. That’s one of the first things you learn, right after C-g. And if you go back too far you can press M-n to go forward again. End of story, right?

    Not quite! What happens if you press M-n right away, when there’s nothing to go forward to? You might expect an error, but instead Emacs will often insert something useful. That’s what the Emacs manual calls “future history” - a list of things that the command guessed you might want to type next. 1

    A Few Examples

    Here are a few built-in commands where M-n does something handy:

    • C-x C-f (find-file) - inserts the file name at point (if there’s one) and after it the name of the file you’re visiting, which is great when you want to open one of its siblings
    • M-% (query-replace) - inserts the symbol at point and after it the last thing you searched for with isearch
    • M-s o (occur) - the same suggestions, plus the active region
    • M-! (shell-command) - inserts the name of the file you’re visiting (or the file at point in Dired), which is super handy for commands that operate on the current file
    • C-x b (switch-to-buffer) - inserts the default buffer and afterwards keeps going through the rest of the buffers

    The last one shows something that applies to every command that uses completing-read. When you run out of default values, M-n starts going through the completion candidates themselves. So in a pinch M-n is a poor man’s candidate cycling, even if you’re not using any fancy completion framework.

    Tip: A lot of prompts show their default value (e.g. Describe function (default car):) and most people just press RET to accept it. If the default is almost what you want, though, press M-n to insert it into the minibuffer and edit it there.

    Using it in Your Own Commands

    If you’re writing Emacs Lisp you get all of this pretty much for free. The functions that read from the minibuffer (read-string, read-from-minibuffer, completing-read, read-shell-command and friends) accept a default value, and that default value can also be a list. The first element is the default and M-n will go over the rest of them in order:

    (defun my-search (term)
      (interactive
       (list (read-string
              "Search for: " nil nil
              (delq nil
                    (list (thing-at-point 'symbol t)
                          (add-log-current-defun)
                          (and buffer-file-name
                               (file-name-base buffer-file-name)))))))
      (message "Searching for %s" term))
    

    Here M-n will first offer the symbol at point, then the name of the current function and finally the name of the current file. There’s also read-regexp, which adds a bunch of standard suggestions (the ones you saw in occur) to whatever you give it.

    That’s the trick behind most of the smarter prompts in the upcoming Projectile 3.5. I like this approach a lot, because the suggestions don’t get in anyone’s way - if you never press M-n the prompt behaves exactly as it always has.

    Lazy Suggestions

    There’s one catch with the list approach - you have to compute all the suggestions before the prompt shows up, even though most of the time nobody will ask for them. If they are expensive to compute that’s a problem. Enter minibuffer-default-add-function. Emacs calls it the first time M-n runs out of default values, and whatever it returns becomes the new list of defaults:

    (minibuffer-with-setup-hook
        (lambda ()
          (setq-local minibuffer-default-add-function
                      (lambda ()
                        (append (my-expensive-guesses)
                                (minibuffer-default-add-completions)))))
      (completing-read "Find file: " files))
    

    minibuffer-default-add-completions is what this variable is set to out of the box, and it’s the reason why M-n goes through the completion candidates in the first place. Here we just put our own guesses in front of them. Keep in mind that the function gets called with the minibuffer as the current buffer, so if your guesses depend on the buffer the command was invoked from (e.g. you want the thing at point) you’ll have to switch to it first. (with-selected-window (minibuffer-selected-window) ...) does the job. That’s how find-file comes up with the file name at point, by the way, and I’m using the same trick for Projectile’s projectile-find-file.

    That’s all I have for you today. Give M-n a try the next time Emacs asks you something - you might be pleasantly surprised! Keep hacking!

    1. See C-h f next-history-element and the Minibuffer History section of the manual. ↩

  • Smarter Minibuffer Prompts in Projectile

    Projectile asks you a lot of questions. What do you want to search for? Replace it with what? Which command should I run? For as long as I can remember those prompts were about as dumb as a prompt can be - an empty line, maybe with the symbol at point as the default, and you had to type the rest.

    That’s a bit silly when you think about it, because by the time Projectile asks you something it already knows quite a lot - the project you’re in, the function your cursor is sitting in, the Makefile at the root of the project and what’s in it. I’ve been meaning to do something about this for ages, and in the upcoming Projectile 3.5 a bunch of prompts finally start to use that knowledge. Let’s go over them!

    The Search Prompt

    Every 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 a plain text prompt, so whatever you type is what gets searched for, but it has learned 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 a history 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 search prompt going through its suggestions with M-n and then completing a typed prefix against the project's identifiers with TAB

    The TAB completion is my favorite part. I can never remember whether something was named projectile-replace-review or projectile-review-replace, and now I don’t have to! The candidates are sorted by how often they show up in the project, 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. That takes a few milliseconds on a small project and a fraction of a second on something as big as Emacs itself. Not too shabby, right? You need to type at least 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 using completing-read. That would have been the easiest way to get completion, but with something like Vertico pressing RET would then select the current candidate instead of what you’ve typed, and I really don’t want a search prompt that second-guesses my input. That’s why it’s a plain read-from-minibuffer with a completion-at-point function installed in it, so TAB works the same way it does in a code buffer. In the screencast above the candidates are displayed by consult-completion-in-region. Without it you’ll get the stock *Completions* buffer.

    The Replace Prompts

    projectile-replace reads what to replace with the same prompt, so everything above applies there as well. When it asks what to replace it with, M-n brings up the original text. After all, most replacements are a small edit of the thing they replace (e.g. a rename), so it’s nice not to have to type it again.

    With a prefix argument the search and replace commands ask which files to work on, and those prompts now complete over the file extensions that are actually present in the project. (the most common ones come first)

    The Command Prompts

    The compile, test and run prompts got the same treatment. M-p has always gone through the commands you’ve run before. Now M-n goes the other way - through commands you haven’t run yet, but the project suggests - the default command for the project’s type, followed by the commands of its tasks (Make targets, npm scripts, just recipes and so on). The tasks named after what you’re doing come first, so the test prompt starts with things like make test:

    The test command prompt going from npm test through the project's test tasks with M-n

    The same suggestions show up on M-n when you run a shell command in the project root with s-p ! or s-p &.

    Filtering Search Results

    Projectile’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 type pretty 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 the top-level directories that have matches, busiest first. Getting rid of all the matches in the tests is D RET:

    Flushing every match under test/ from the search results by accepting the default the prompt suggests

    The Find File Prompt

    projectile-find-file (s-p f) is probably the most used Projectile command, and quite often the file you’re after is one that the current buffer already points to - the module imported on the current line, a file mentioned in a backtrace, the test for the code you’re editing. Projectile has had dedicated commands for all of those forever (projectile-find-file-dwim, projectile-toggle-between-implementation-and-test, projectile-find-other-file), but you have to remember them and their keybindings.

    Now M-n at the plain old find file prompt brings up the file named at point, followed by the test of the current file (or its implementation, if you’re in a test) and its “other” files. After that it keeps going through the rest of the project’s files, as before.

    The find file prompt bringing up the file imported at point with M-n and then the test of the current file

    Projectile is pretty relaxed about what counts as a file name at point - a bare name, a relative path, an import without the file extension, something like cart.js:42 in a compilation buffer - they all work.

    Note: Those are suggestions on M-n and not the default value of the prompt for a reason. Completion UIs like Vertico move the default to the top of the candidate list, and that spot belongs to the file you’ve been visiting the most recently and the most often. Pressing RET right away should keep doing what it has always done.

    Odds and Ends

    • A few prompts that offer a handful of options now have a sensible default. projectile-switch-to-buffer defaults to the project buffer you were in last (just like C-x b), projectile-run-task to the task you ran last, and projectile-find-file-in-subproject to the subproject of the current file.

    • C-u s-p C (projectile-find-changed-file with a prefix argument) asks for a revision 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 Underrated

    You’ve probably noticed that M-n shows up in pretty much every section of this article. Every Emacs user knows that M-p in the minibuffer goes back through the history. I’m not sure how many people know that M-n goes forward, into what the Emacs manual calls “future history” - a list of guesses that the command has made about what you might want to type. A lot of built-in commands provide those (try M-n at the query-replace or find-file prompts), 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 be better 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 a list 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, and it’s only a few lines of code. I wrote a bit more about M-n here, as it certainly deserves an article of its own.

    Closing Thoughts

    None of those changes is a big deal on its own, but together they make Projectile feel a lot smarter to me. All of this will be a part of Projectile 3.5, and it’s already available in the master branch if you want to play with it today. If you can think of other prompts that could do a better job of guessing what you want - I’d love to hear about them!

    That’s all I have for you today. Keep hacking!

  • Emacs Prelude 2.2

    Emacs Prelude 2.2 is out! 2.0 and 2.1 shipped within a few days of each other back in March, while 2.2 took eight months and about 150 commits. It’s a big one!

    Read More
  • Working with Git Worktrees in Magit

    I’ll admit that until fairly recently I had no idea git worktrees existed. They’ve been part of git for a decade1 and I never once needed them. Feature branches did the job just fine - create a branch, do the work, merge it, delete it, and start over.

    What finally introduced me to worktrees was, of all things, AI coding agents. Tools like Claude Code create a worktree for each task, so several agents (or several sessions of the same agent) can work on the same repo in parallel without stepping on each other - or on you. Suddenly my projects directory was full of cider-this and cider-that siblings, and I figured I should understand what’s actually going on there.

    1. They were introduced in git 2.5, released all the way back in July 2015. ↩

    Read More

Subscribe via RSS | View Older Posts