Loading ...
Sorry, an error occurred while loading the content.

47655Re: Preserve case during search & replace?

Expand Messages
  • Paul Brinkley
    Feb 10, 2004
      At 01:40 PM 2/10/2004 -0600, gumnos \(Tim Chase\) wrote:
      >> Basically, I'd like to replace (e.g.) "apple" with "banana"
      >> everywhere in a block of text, but if "apple" happens to be
      >> capitalized, it should be replaced with "Banana". In other
      >> words, I'd like a handy way of doing
      >> :s/Apple/Banana/g
      >> :s/apple/banana/g
      >> (assuming noignorecase) with one command.
      >I've toyed with this idea before, even asking the list at one point.
      >However, you hit cases (including your own example) where the search-regex
      >and the replacement don't line up correctly. eg.
      > :s/ApplE/FOO/g
      >What's the replacement "FOO"? would it be "BanaNa" or would it be
      >"BananA" (is that a "make the 5th letter uppercase", or is it a "make the
      >last letter uppercase"). It gets worse, if you use camel-case:
      > :s/HelloWorld/SomeText/g
      > :s/helloworld/sometext/g
      > :s/helloWorld/someText/g
      >is likely what you want, but with the below suggestion, you end up with
      >something more like
      > :s/HelloWorld/SometExt/g
      > :s/helloworld/sometext/g
      > :s/helloWorld/sometExt/g

      Harumph - yeah, this had crossed my mind while I was typing my message,
      and then I forgot to actually mention it. My inclination in most of these
      cases is to simply punt. The feature I'm looking for should effectively
      be a narrow special case of :s applicable only to the following:


      where ~ is the flag that turns this on (best flag I could think of;
      suggestions?). It works ONLY on the first character. My rationale for
      this is simply based on the places where I use it: in code. Specifically,
      Java code, though I would find this useful in C, C++, and others. Just
      today I had code that looked like


      I wanted to replace "close" with "cancel" everywhere. I can't see having
      to worry about any occurrences of "CLOse", "ClosE", etc. I can certainly
      see the argument for wanting to preserve a form of elegance to the feature,
      however. (The last thing I want is a warty Vim.)

      I didn't think of this before, but I might also want to replace "CLOSE"
      with "CANCEL":

      public static final CLOSE_BUTTON_TEXT = "Close";

      So that's three cases, all relatively clear-cut. I can think of no others
      in common use. (Can you?) Realistically, what would you do if you saw
      "CLOse" or "CLoSe"? What ought to be done? Confirm replace? Just skip it?

      >Hope this sets you out in a helpful direction,

      The snippets certainly do, indeed. I still say this ought to have native
      Vim support. :-) But at the very least, there's a lead here on a nice
      script to upload to the website.

      The spec I'm seeing is something like this:


      The ~ flag expands the search to the following (noignorecase implied):


      Semantically, this appears clear enough to me. I'd of course be
      concerned with how generally useful this would be. For instance,
      how universal is the above usage in coding in languages other than
    • Show all 22 messages in this topic