user54712
user54712

Reputation:

Vim Case-Agnostic Regex

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?

Upvotes: 10

Views: 545

Answers (2)

Ethan Winn
Ethan Winn

Reputation: 153

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.

Upvotes: 1

dF.
dF.

Reputation: 75785

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

Upvotes: 10

Related Questions