Skip to content

IEdit: Interactive, multi-occurrence editing in your buffer

by mickey on October 2nd, 2012

Have you ever heard of iedit for Emacs by Victor Ren (Update:Seems the github link I posted earlier was way out of date)? 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, like isearch, and 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 of word 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 code below aim to do just that: when invoked, it will take the word at point and only iedit occurrences in the local defun (Note: don’t forget that although defun is Lisp-speak, most modes automatically support commands like 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.

Update: Le Wang pointed out that I was using an older version of iedit; the code has been updated to reflect the API changes.

29 Comments
  1. I’ve been using mark-multiple to achieve this effect.

  2. Alexander Vorobiev permalink

    mark-multiple has been superseded by multiple-cursors.el (same author).

    • mickey permalink

      Interesting library. I’ll have to play around with it too. From RTFM I can see you have to explicitly place marks where you want it to edit, no? If so, iedit does serve a niche not easily filled by multiple-cursors in that it blithely picks the word under point.

      Mickey.

      • mc/mark-next-like-this will mark the next text matching the region.

        mc/mark-all-like-this will mark all occurrences that match the region.

    • Multiple-cursors is nice cause you can either select next occurrence of region or all occurrences. Then you have multiple cursors at the regions. Then you can do anything you want with the cursors.

      Checkout author’s Emacs Rocks video on multi-cursors. Its freaking amazing what he does with them

      http://emacsrocks.com/e13.html

  3. Le Wang permalink

    Victor Ren’s repo is at https://github.com/victorhge/iedit . You’ve linked to a 2 year old version of it and the last commit was 23 days ago.

  4. Da Zhang permalink

    Your Emacs interface looks cool. What’s the color theme and font you use?

    • mickey permalink

      Hello

      I don’t use color themes; never have, and I probably never will: I picked the colours over 10 years ago and I have never changed them :)

      The font is “Consolas”

      Mickey.

  5. Alex permalink

    Thanks a lot and nice style, btw.

  6. Thanks! I had tried multiple-cursors.el but it didn’t work nicely with my other customizations, so I was very happy to hear about iedit, which works well for me.

  7. Mikef permalink

    Having trouble limiting scope of changes to a region, trying to follow the docstring. Here is what I am doing
    Assumptions
    *transient mark mode, cua mode both active
    *C-= is bound to iedit-mode
    *emacs 24.2

    1) Move mark to a symbol that I want to manipulate
    2) C-= (all occurrences are highlighted ok so far)
    3) Set mark, forward line 5x (I am creating a region)
    4) Type a character (I want to change the symbol occurrence)

    *Region is replaced by that character.
    *Expected multiple symbols to be replaced

    • mickey permalink

      Don’t think you can do it by marking a region.

      You have to use narrow-to-region (C-x n n) or just use my handy little helper.

    • Mikef permalink

      narrow-to-region worked well interactively. Tried to modify your defun to work on a region; not having much luck.
      Why does this not work?
      Thanks
      Mike

      (defun iedit-dwim (arg)
      “Starts iedit but uses \\[narrow-to-region] 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-region (point) (mark))
      (iedit-start (current-word) (point-min) (mark)))))))

  8. What color theme are you using on that screenshot? Very nice.

    • mickey permalink

      No color theme; never used them. This is my own scheme I’ve tweaked over the years.

  9. I’ve been using multiple-cursors for some time now and it’s fantastic, you can select some occurrences of a word or all and even more.

  10. Le Wang permalink

    You should check out the recent iedit changes. Symbol selection, rectangles, restricting after selectiing.

    To do what your command is doing for symbols use C-; C-M-h C-;

  11. Kolya Ay permalink

    Sorry for off-topic, but what colour theme do you use?

    • mickey permalink

      No color theme; never used them. This is my own scheme I’ve tweaked over the years.

  12. This version of iedit seems to have this functionality already: https://github.com/victorhge/iedit (bound to C-h C-; I guess)

  13. Never mind the previous comment, I was reading from the Google Reader feed, which had the old copy of the article. It seems like you have already fixed the link.

  14. iedit is good. but you iedit-dwim is such a awesome tool.

  15. your color themes is so beautiful, what is it?

    thanks a lot

    • mickey permalink

      Hello,

      I don’t use colour themes; this is a personal customization I set up a long, long time ago :)

      Mickey.

      • Hi Mickey, do you think you could post the output of

        grep ‘-face’

        From your .emacs / customize section? I’d be happy to turn your color selection into an Emacs 24 theme for you.

  16. > iedit is a lot dumber as it cannot infer context beyond I want to iedit all occurrences of word point is on.

    Per iedit’s docs:

    > with digit prefix argument 0, only occurrences in current function are matched. This is good for renaming refactoring in programming.

    • mickey permalink

      That’s hardly context beyond what is offered by narrowing to defun.

      The word “context” means that it can tell the difference between “iediting” a variable called “foobar” and a method also named “foobar”.

Trackbacks & Pingbacks

  1. Personalising my toolset: Konsole, BASH, Emacs, weechat, tmux.. « bits from /home/gene

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS