If I had something like a list of #defines in a c file, say

#define a
#define cd
#define blah

and I wanted to replace all the #defines to capitalized characters, can I do this in a single substitution command? Maybe with regex?

I'm thinking something along the lines of :%s/a/A, but more of a general command so that I can apply it just once and it gets used on all of the #defines.

2 Answers 2


You can use the \U flag in substitutions to substitute the uppercase version. This will work for the first word on every line:


This uses memory parentheses \( \) to capture the first word and then \1 in the substitution to "remember" what we've captured.

If you want to get words that aren't at the beginning you might just have to use the word itself:


See :help s/\U for more info.


Your question is very close to this one: How to execute command on every matching pattern, not just lines?

The difference is that you don't want to remove the next word, but to change it to uppercase.

Mixed with @Tumbler41's solution that changes define into DEFINE, it becomes:


This time define foo becomes define FOO

Note that thanks to \zs we don't need a capture group here. We can use & which is similar to \0: it contains the complete text matched -- except the context that is matched with a zero-width (\zs)

Of course all other solutions will work: you can use gUw instead of dw, but I'm a :%s guy, hence my answer :)

Your Answer

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

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