Roy Fulbright <[email protected]> [09-11-22 15:36]:
> 
> 
>  
> 
> 
> 
> 
> 
> > Date: Sat, 21 Nov 2009 07:21:44 +0100
> > From: [email protected]
> > To: [email protected]
> > Subject: smartcase s'n'r even smarter?
> > 
> > 
> > 
> > Hi,
> > 
> > in my .vimrc I set smartcase on.
> > 
> > In my file I have (for example) the following words
> > 
> > Uppercase
> > uppercase
> > UPPERCASE
> > 
> > Doing a 
> > 
> > :s/uppercase/frankenstein/g
> > 
> > produces
> > 
> > frankenstein
> > frankenstein
> > frankenstein
> > 
> 
> Sorry I didn't initially understand your question. I do not have a 
> Vim-specific answer, but at the risk of being a bit OT here is a Perl 
> solution that maintains existing case of all characters in the source data 
> during a search and replace operation. Hope this helps.
> 
>  
> 
> Best regards,
> 
> Roy Fulbright
> 
>  
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> ########################################
> #  Name: keepcase.pl
> #  Desc: Transfer case of search string to replacement string.
> #
> #  Explanation of values and symbols:
> #  ----------------------------------
> #  $old - search string.
> #  $new - replacement string.
> #  $1   - current value of search string in regex.
> #  ^    - XOR (bit is true if and only if one of the two bits is true)
> #
> #  Expression Evaluated In Regex Replacement String:
> #  -------------------------------------------------
> #  lc $new # lowercase the replacement string
> #  ^ $1    # XOR with $1 - increase replacement string character values
> #          # by corresponding ascii value of each character of $1.
> #  ^ lc $1 # XOR with lc $1 - subtract the ascii values of lowercase $1.
> #          # If both values were lowercase to begin with, the resulting
> #          # sum is 0, otherwise the increase is enough to uppercase the
> #          # corresponding replacement string character.
> ##############################################
> 
>  
> 
> my $old = 'perl';
> my $new = 'grep';
> my $txt = 'Remember, perl is the program that runs Perl, not PERL, not PeRl, 
> and not pErL.';
> 
>  
> 
> print "\n   *** change '$old' to '$new' while maintaining case ***\n";
> print "\nBefore: $txt\n";
> $txt =~ s/\b($old)\b/lc$new^$1^lc$1/gie;
> print " After: $txt\n\n";
> 
>  
> 
>  
> 
> 
> 
> 
> 
> Hotmail: Trusted email with powerful SPAM protection. Sign up now.
>                                         
> _________________________________________________________________
> Windows 7: I wanted simpler, now it's simpler. I'm a rock star.
> http://www.microsoft.com/Windows/windows-7/default.aspx?h=myidea?ocid=PID24727::T:WLMTAGL:ON:WL:en-US:WWL_WIN_myidea:112009
> > 

Hi Roy, 

Thank you very much for the script. Unfortunately I cannot use it at
work since I am not allowed to install Perl (or anything else) on the
Windows PC I have to use.

But at home! TADA! I am using Linux here and no problem with Perl!

Have a nice sunday!
Best regards,
mcc


-- 
Please don't send me any Word- or Powerpoint-Attachments
unless it's absolutely neccessary. - Send simply Text.
See http://www.gnu.org/philosophy/no-word-attachments.html
In a world without fences and walls nobody needs gates and windows.


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to