The Mastering Emacs ebook cover art

Emacs 28 Edition is out now!

Read a Free Sample

Learn More

IEdit: Interactive, multi-occurrence editing in your buffer

IEdit is a simple tool that creates multiple, distinct cursors in your buffer that move and edit in perfect harmony. IEdit is perfect for bulk editing common items, like identifiers and other common symbols found in programming or text.

Have you ever heard of iedit for Emacs by Victor Ren? Me neither, until recently, and that’s a terrible shame as it is a cornerstone of my programming workflow now that I’ve learned about it.

So what does it do, then?

Well, quite simply, you can edit multiple, identical string occurrences at the same time. The twist here is it uses in-buffer editing without disrupting your workflow with prompts, windows or any of that stuff. You plonk your point down on a word you want to change; you run iedit-mode; and now all the occurrences of that word is highlighted on your screen. When you alter a highlighted word, the other highlighted words change also. How cool is that? Modern IDEs have it already – usually hidden away in the “Refactoring” section – and does exactly the same thing, but iedit is a lot dumber as it cannot infer context beyond I want to iedit all occurrences ofword* point is on.*

If you regularly replace variables or words with M-% or C-M-% — well, you can retire that workflow now, as iedit will handle it for you. Sure, go ahead; use the older way if you have complex, partial replacements you want to do, but if you’re renaming a variable in a buffer… Why not use iedit?

Here’s a sample demonstration showing what exactly it is I’m talking about, in brilliant technicolor:

Emacs with IEdit active

Improving iedit

So iedit’s pretty great and all that, but I don’t replace words across a whole buffer very often; sure, I hear you say: “just narrow-to-defun with C-x n d!” Indeed, narrowing’s great, but this blog is all about half-baked, half-inventions and cobbled-together scripts, and this post is no exception!

I prefer a workflow that minimizes the use of commands to do routine tasks – a fairly common goal for most Emacs hackers. The snippet below does just that. When invoked, it activates iedit for the current defun only.

Note: Don’t forget that although defun is Lisp-speak, most modes support all the defun-like commands such as mark-defun or narrow-to-defun.

If you pass an argument to the function, it will iedit all occurrences in the entire buffer.

The iedit author suggest that you bind iedit-mode – the default command for entering iedit – to C-; and I agree: it’s rarely used and easy to type.

(require 'iedit)


(defun iedit-dwim (arg)
  "Starts iedit but uses \\[narrow-to-defun] to limit its scope."
  (interactive "P")
  (if arg
      (iedit-mode)
    (save-excursion
      (save-restriction
        (widen)
        ;; this function determines the scope of `iedit-start'.
        (if iedit-mode
            (iedit-done)
          ;; `current-word' can of course be replaced by other
          ;; functions.
          (narrow-to-defun)
          (iedit-start (current-word) (point-min) (point-max)))))))



(global-set-key (kbd "C-;") 'iedit-dwim)

Further Reading

Have you read my Reading Guide yet? It's a curated guide to most of my articles and I guarantee you'll learn something whether you're a beginner or an expert. And why not check out my book?

Subscribe to the Mastering Emacs newsletter

I write infrequently, so go on — sign up and receive an e-mail when I write new articles