10

I've run into the following case a few times and I was wondering if there is a fast way to handle it in Vim.

I'll have a source file like the following:

#ifndef _FOO_H_ #define _FOO_H_ class Foo { Foo(int foo); }; #endif 

And I would like to convert it to the following:

#ifndef _BAR_H_ #define _BAR_H_ class Bar { Bar(int bar); }; #endif 

So, I want all foo -> bar, but to keep the capitalization of the original. Right now, I've been doing this with 3 or 4 different regexes, but it seems there should be a better way. Any ideas?

2 Answers 2

10

This script will do a case-preserving search/replace.

Copy the script to the plugin directory (~/.vim/plugin), then do

:set ic :%s/foo/\=KeepCaseSameLen(submatch(0), 'bar')/g 
Sign up to request clarification or add additional context in comments.

1 Comment

You should update your version of keepcase. I've added a :SubstituteCase command to it that simplifies substitutions a lot. :)
1

Tim Pope's Vim Abolish plugin can do this and a whole ton more. :%Subvert/string/new string/g will preserve case, capitalization, etc., and the plugin also supports word variants using a {var1,var2} syntax. Very useful.

Comments