sponsor Vim development Vim logo Vim Book Ad

SmartCase : replacing words while keeping original lower/uppercase style

 script karma  Rating -316/359, Downloaded by 1834  Comments, bugs, improvements  Vim wiki

created by
Yuheng Xie
 
script type
utility
 
description
An example, you may want to replace any FileSize appears in your program into LastModifiedTime. Since it appears everywhere as both uppercases and lowercases, you have to write it several times:

  :%s/FileSize/LastModifiedTime/g      " function names
  :%s/file_size/last_modified_time/g   " variable names
  :%s/FILE_SIZE/LAST_MODIFIED_TIME/g   " macros
  :%s/File size/Last modified time/g   " document/comments
  ......

This script copes with the case style for you so that you need write just one command:

  :%s/file\A\?size/\=SmartCase("LastModifiedTime")/ig

An alternative way:
  first search for the string:  /\cfile\A\?size
  then run a command:  :%SmartCase "LastModifiedTime"

By the way, SmartCase can also cope with the circumstance where you want to replace the string's case style while keeping its words. For example:
  FileSize => file_size
  LastModifiedTime => last_modified_time
  ......
This can be done with the following command:

  :%s/\(\u\l\+\)\{2,}/\=SmartCase(0,"reference_style")/g

To sum up: the first argument to SmartCase is the reference words, the secend argument is the reference styles, if the second argument omitted, it's submatch(0).
 
install details
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
smartcase.vim 1.0.3 2017-05-16 6.0 Yuheng Xie Trivial improvement: Treat numbers using last style, and will not lose numbers when replacing.
smartcase.vim 1.0.2 2006-01-11 6.0 Yuheng Xie changed <q-args> to <args> for multiple arguments capability.
changed some usage help.
smartcase.vim 1.0.1 2005-09-20 6.0 Yuheng Xie 1) Added a command interface :SmartCase
2) a bug fixed
Thank Salman Halim for the bug report and advices.
ip used for rating: 18.221.187.121

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github