How can I swap or replace multiple strings in code at the same time?

前端 未结 5 1287
北恋
北恋 2021-01-12 10:32

Given the following code sample:

uint8_t i, in, ni;
i = in = 2; ni = 1;
while (2 == i > ni) in++;

How can I replace i, in, and ni<

5条回答
  •  深忆病人
    2021-01-12 11:06

    Shorter Emacs solution:

    C-M-% (query-replace-regexp)

    To match: \<\(i\|in\|ni\)\> (I assume you want to match whole words only)

    Replace with: \,(case (intern \1) (i "in") (in "ni") (ni "i"))

    You'll need to require 'cl at some point before doing this to get the case macro from the CL package. You could achieve the same effect without that package, but it wouldn't be as terse.

    EDITING TO ADD: Actually, it could be as terse, as I realized when answering a similar question on Reddit recently.

    To match: \<\(?:\(i\)\|\(in\)\|ni\)\>

    Replace with: \,(if \1 "in" (if \2 "ni" "i"))

提交回复
热议问题