Vim tricks. Changing the case when replacing

Video manual about changing the case when replacing in VIM

\U - if used in the replacement scheme, then everything after it is written in uppercase

\L - lowercase characters after it

\u - uppercase letters in the first character of what follows it

\l - omits the first character of the character following it

\e or \E - stops uppercase or lowercase letters

How can we use them?

For example, we can find all email addresses and convert them to lowercase:

:%s/\S\+@\S\+/\L&/

The key part here is \L before &. This means that in the future we will write everything in lowercase.

Here's another option. Let's say we want to make the part of the email address containing the name lowercase, but leave the case of the company name intact. Thus, [email protected] It will become [email protected] . In this case, we need to use \e (or \E) to complete the character case manipulation halfway through the replacement pattern. The final replacement will look like this:

:%s/\(\S\+\)\(@\S\+\)/\L\1\e\2/g

The important point here is substitution. We use \L to start lowercase writing, then \1 for the first capture (e.g. FirstLast), then \e to end lowercase writing, and then \2 for the second capture (e.g. @Company.com ). As a result, the first part of the email address will be highlighted in lower case, and everything else will remain unchanged.