37

I have a file with this in it:

test
Test

Currently, if If I want to replace all cases of 'test' with 'foo', and 'Test' with 'Foo', I have to use two commands:

:%s/test/foo/g
:%s/Test/Foo/g

Is there any way that I can accomplish this with a single command?

3
  • I'm not sure if this is quite what you need: :%s/test/foo/g | %s/Test/Foo/g May 28, 2012 at 13:31
  • Yes that works, but doesn't really help me out as I am trying to speed up the process. May 28, 2012 at 13:33
  • Haha. Yeah... :) That is what I generally do, but I didn't feel 100% about providing it as an answer because, it being Vim, there's probably some other solution which will be super concise and awesome. May 28, 2012 at 13:38

2 Answers 2

16

I think this vim plugin by tim pope will solve your problem, install it and %Subvert/{t}est/{f}oo/g or %Subvert/test/foo/g (thanks for the comment) will do the trick

1
  • The plugin works, although you only need to type %Subvert/test/foo/g May 28, 2012 at 13:57
13

As my granpappy used to say, for every plugin there's a vim one-liner that's just as good. Try

%s/\(test\|Test\)/\=submatch(0) =~ '^\l.*' ? 'foo' : 'Foo'/g

Explanation:

%s/ substitute over the whole buffer

\(test\|Test\)/ match for 'test' OR 'Test'

\= Make the substitute string use whatever the following expression evaluates to

submatch(0) In this context evaluates to the entire matched expression

=~ '\l.*' String-comparison for regex match (against a word starting with a lowercase letter)

... ? 'foo' : 'Foo' if-then-else expression which evaluates to 'foo' if ... is true, 'Foo' otherwise

/g Do this over the entire line

With slight modifications this should be more than enough to satisfy our needs. References:

:help :s
:help :sub-replace-\=
:help expression

...I just want to put a little warning for anyone who tries to get into vim evaluations and expressions... They are very counter-intuitive (I come from a C++ background, maybe if you come from bash or python it's less of an issue). I found the following vimscript guide/ self-teaching lessons to be extremely helpful: http://learnvimscriptthehardway.stevelosh.com/

4
  • 1
    This is very cool but for some reason I can only get it to work if I use \u instead of \l (and then swap the two sides of the colon). When I use \l, it only uses the lowercase substitution.
    – NeRoboto
    Jun 8 at 15:57
  • @NeRoboto weird. What version of vim? Jun 11 at 18:54
  • 1
    VIM - Vi IMproved 9.0 (2022 Jun 28, compiled May 13 2023 12:55:09) macOS version - arm64
    – NeRoboto
    Jun 11 at 20:08
  • 1
    @NeRoboto Thanks for the catch - it was most likely my error (I tried on versions 7.4, 8.0, and 9.whatever). I'm pretty sure that I tested it using the strings test and TEST, then changed it here to better match the question and did not retest. Your workaround will work as long as there are no capital letters in the "lowercase option." Otherwise, for the matching I guess it's correct to indicate "starting with lowercase" as ^\l (or ^\u for uppercase). I've updated my answer. Jun 12 at 8:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.