Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM: Select a regex, then apply a command

Tags:

regex

text

vim

I want to change a large group of identifiers from lower-case to upper-case. I have a file with a number (hundreds or so) of unique identifiers that start with a q_. I constructed a regex that defines this match: (q_\w*) and now I want to apply the ~ command to make them all upper-case (they are currently lower-case). I feel like VIM is more than capable of doing this but my skill with it is not there yet, can you help?

like image 720
steve_gallagher Avatar asked Oct 03 '12 19:10

steve_gallagher


1 Answers

If you want to up-case the entire identifier, for each identifier in the file, use this substitute command:

%s/q_\w\+/\U&/g

The trick here is & in the replacement pattern, which references the entire match. This will substitute e.g. q_identifier1 with Q_IDENTIFIER1.

If you only want to up-case the q, then you can use:

%s/q_\(\w\+\)/Q_\1/g

Now q_identifier1 will be changed to: Q_identifier1

If you want to up-case everything except the q_, then use:

%s/q_\(\w\+\)/q_\U\1/g

Now q_identifier1 will be changed to: q_IDENTIFIER1

like image 179
pb2q Avatar answered Nov 15 '22 05:11

pb2q