Re: can :s imitate case of match when replacing?

2006-07-15 Thread Maciej Kalisiak

On 14/07/06, Marshall Abrams <[EMAIL PROTECTED]> wrote:

I just asked the same question recently.  The following email includes
all of the recent discussion:

From: [EMAIL PROTECTED]
Subject:Re: auto upper/lower in replace pattern based on search
pattern?
Date:   July 13, 2006 1:19:24 PM EDT
To:   vim@vim.org

Thanks--keepcase.vim is great.  So now with
:%s/firstname/\=KeepCase(submatch(0), 'LastName')/ig
I can replace all instances of
firstname with lastname
firstName with lastName
FirstName with LastName

But \=KeepCase(submatch(0), '') is a mouthful.
Not a problem; that exact expression does what I'll want 99% of the
time, so I've already mapped it to a control-key.  And there are other
ways to abbreviate.

Still, this seems *such* a useful function in a programmer's editor, it
seems worthwhile to build it into the :substitute command as some kind
of optional behavior.

How about one of these:

:s/firstname/LastName/k ['k' for keepcase]

:set MaGiC

or some kind of delimiter that can be stuck into a pattern to say "use
KeepCase() on this part".  OK, I know there aren't many delimiters that
are available at this point.


Thanks Marshall!  Yes, I too was looking for a script-less approach,
something built-in.  I'll probably end up using keepcase.vim that
Yakov helpfully pointed out, but it just seems odd for this not to be
a built-in functionality.  Surely this is a commonly occuring problem:
s/foo/bar/g , where
- in regular text, foo may sometimes appear at beginning of sentences,
capitalized
- in source code, appearing as FOO (#define), foo (regular variable),
MyFoo (class name in camelcase))

I constantly run into this problem when refactoring code, when
choosing more appropriate and descriptive names for concepts and
classes.  Initially, when I started looking for this feature, I
totally expected to see it as a flag to :s.  It's the most obvious
place for such a feature, IMHO.  Hence I totally agree with, and would
like to add my voice/vote to yours, on your proposed "k" flag. :)


Re: can :s imitate case of match when replacing?

2006-07-14 Thread Yakov Lerner

On 7/14/06, Maciej Kalisiak <[EMAIL PROTECTED]> wrote:

Is it possible to have the :s substitute command to imitate the case
of the match when substituting?  For example, with a single
:%s/foo/bar/i  I'd like the following to happen:

matched  ->  desired
foo -> bar
Foo -> Bar
FOO -> BAR

I looked at :h :s_flags, but nothing seems to fit...


Try this script:
  http://www.vim.org/scripts/script.php?script_id=6
  keepcase.vim : Functions for doing case-persistant substitutions

Yakov