0

How can I replace a block of code with another, akin to query-replace/query-replace-regexp?

For example:

Replacing the following block:

for(A) {
  if(B) {
     s1;
  } else {
     C;
  }
}

with

for(M) {
  if(N) {
    s1;
  } else {
     O;
  }
}
1

2 Answers 2

1

You could use iedit for that.

enter image description here image source: iedit github repository

You should select your block of code and activate iedit-mode (C-;) to edit it.

When Iedit mode is turned on, all the occurrences of the current region in the buffer (possibly narrowed) or a region are highlighted. If one occurrence is modified, the change are propagated to all other occurrences simultaneously.

If region is not active, ‘iedit-default-occurrence’ is called to get an occurrence candidate, according to the thing at point. It might be url, email address, markup tag or current symbol(or word).

In the above two situations, with digit prefix argument 0, only occurrences in current function are matched. This is good for renaming refactoring in programming.

After install iedit-mode from MELPA, I encourage you to read the full help (C-h f iedit-mode).

2
  • Seems neat. Would check it out. Also, what are you using make the screencasts and displaying the keyboard input?
    – nova
    Oct 27, 2019 at 9:39
  • This gif is not mine, it is from iedit github repo. He used probably command-log-mode (C-c o to show/hide the log); for screencast you could search for "screencast" on list-packages. If you have different questions you should ask them in new questions so others can find them.
    – adl
    Oct 27, 2019 at 18:12
0

The query-replace-regexp does the job.

<code>query-replace-regexp</code> demo:

1
  • 3
    Can you elaborate? Perhaps show or explain how it "does the job".
    – Drew
    Oct 27, 2019 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.