4

Is there a way (or package) to search and replace code like:

class Account
  def list_accounts
  end
end

to

class Company
  def list_companies
  end
end

Basically I want to replace account(s) with compan(y/ies)

Vim does this with Subvert

  • 1
    The "case" part of your question should be the default behaviour of `query-replace`. That is, if you ask to replace "account" with "company", then "Account" will be replaced with "Company". Is that not what happens for you? – Malabarba Sep 29 '14 at 16:57

1 Answers1

7

Retaining the cases on replacing

By default, emacs retains the cases when replacing.

From the emacs manual,

When the newstring argument is all or partly lower case, replacement commands try to preserve the case pattern of each occurrence. Thus, the command

M-x replace-string <RET> foo <RET> bar <RET>

replaces a lower case foo with a lower case bar, an all-caps FOO with BAR, and a capitalized Foo with Bar. (These three alternatives—lower case, all caps, and capitalized, are the only ones that replace-string can distinguish.)

Ensure that the case-replace variable is t (which it is, by default).

Replacing with plurals

You can try query-replace-regexp (C-M-%) then replace

account\(s\)?

with

compan\,(if (string= \1 "s") "ies" "y)
Jonathan Leech-Pepin
  • 4,307
  • 1
  • 19
  • 32