2010-02-08

interactive replacement



I recently found an interesting little package called iedit.el, which allows
you to interactive replacements on multiple strings at the same time. That is,
as you do one replace, you can see how all the same matches being replaced as
you type. It's hard to explain in words, you just have to try it out.

So, how to install this package?

  • First, download the package (see the link above)
  • Put it in a directory where emacs can find it; for example,
    ~/.emacs.d/elisp. If you haven't done so already, you can tell emacs to look in that directory by adding to your .emacs:
    (setq load-path (cons "~/emacs.d/elisp/" load-path))
    
  • Then, tell emacs to load this specific module; you can add
    (require 'iedit)
    
    to your .emacs to load it (alternatively, you could use the autoload facility)
  • then, define a key binding for this:
    (define-key global-map (kbd "C-;") 'iedit-mode)
    
  • Now, search for some strings that appears multiple times in your buffer, select it,
    press C-; (Ctrl+;), and watch how all other matching strings are
    changing at the same time.

Very nice.

10 comments:

Steve Purcell said...

Nice. Also check out all.el, which is like an editable version of occur. This iedit.el will be best once it's properly integrated with isearch.

Steve Purcell said...

Here's are links to all.el [ftp] [http], since it isn't easily googleable.

ivijay said...

I did an 'isearch' for emacs using C-s, and then did C-; and got the following message:

No region select, cannot enable iedit mode

djcb said...

@ivijay -- indeed; isearch does not automatically select the found string, you have to do that still.

I added a note to the article. Exercise for the reader to add the auto-select :-)

Unknown said...

it seems that iedit only work on .el file.
I tried to use C+; in .txt and .org file, it does not work.

Did I miss something?

djcb said...

@forfan: it might be that something is interfering with it... does it work when you use emacs -q?

semola said...

Thanks for the tips ! Is it possible to have this functionality into a block ? For example only replace occurrence in the definition of a particular function ?

djcb said...

@semola: you could use the narrowing for -- see the emacs manual.

Noufal said...

the link to gmane you have in your article is 404ing

Bozhidar Batsov said...

You can try this link instead - http://groups.google.com/group/gnu.emacs.sources/browse_thread/thread/8f4699bb409ff5c5

The latest version is attached to a message from the 1st of February at the moment.