Vim Tips Wiki
(clean up a little)
No edit summary
Tag: sourceedit
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=298
 
|id=298
Line 14: Line 13:
 
There are times that you might like to go through a file and change the case of characters that match some arbitrary criteria. If you understand regular expressions well, you can actually do this fairly easily.
 
There are times that you might like to go through a file and change the case of characters that match some arbitrary criteria. If you understand regular expressions well, you can actually do this fairly easily.
   
It's as simple as placing \U or \L in front of backreferences which you want to change the case of, and \E at the end. Vim will make the text in the backreference uppercase or lowercase (respectively). Use \u and \l (without the \E at the end) to just change the case of the very first character in the backreference.
+
It's as simple as placing \U or \L in front of backreferences for the desired conversion. Without the \E directive, the entire backreference, and any additional text, will be converted. The \E directive terminates the conversion to the upper/lower case directive. Vim will make the text in the backreference uppercase or lowercase (respectively). Use \u and \l (without the \E at the end) to just change the case of the very first character in the backreference.
   
  +
For example:
(A "backreference" is a part of a regular expression that refers to a previous part of a regular expression. The most common backrefernces are &, \1, \2, \3, ... , \9).
 
  +
Assume a file with this text, "This is a test"
  +
<pre>
  +
:s/\(test\)/\U\1 file/
  +
</pre>
  +
Produces: This is a TEST FILE
  +
<pre>
  +
:s/\(test\)/\U\1\e file/
  +
</pre>
  +
  +
Produces: This is a TEST file
  +
 
(A "backreference" is a part of a regular expression that refers to a previous part of a regular expression. The most common backreferences are &, \1, \2, \3, ... , \9).
   
 
Some examples that demonstrate the power of this technique:
 
Some examples that demonstrate the power of this technique:
Line 50: Line 61:
 
Not rocket science, but otherwise you'd have to do this:
 
Not rocket science, but otherwise you'd have to do this:
 
:%s/\(select\)\|\(order)\|\(by\)\|\(from\)\|\(where\)/\U\1\U\2\U\3\U\4\U\5/g
 
:%s/\(select\)\|\(order)\|\(by\)\|\(from\)\|\(where\)/\U\1\U\2\U\3\U\4\U\5/g
  +
  +
[edit: Much easier to just use this, where either 0 or 1 will work:]
  +
:%s/\(select\|order\|by\|from\|where\)/\U\1/g
 
</pre>
 
</pre>
   

Revision as of 23:21, 9 November 2015

Tip 298 Printable Monobook Previous Next

created August 5, 2002 · complexity intermediate · author Jonathan McPherson · version 5.7


There are times that you might like to go through a file and change the case of characters that match some arbitrary criteria. If you understand regular expressions well, you can actually do this fairly easily.

It's as simple as placing \U or \L in front of backreferences for the desired conversion. Without the \E directive, the entire backreference, and any additional text, will be converted. The \E directive terminates the conversion to the upper/lower case directive. Vim will make the text in the backreference uppercase or lowercase (respectively). Use \u and \l (without the \E at the end) to just change the case of the very first character in the backreference.

For example: Assume a file with this text, "This is a test"

:s/\(test\)/\U\1 file/

Produces: This is a TEST FILE

:s/\(test\)/\U\1\e file/

Produces: This is a TEST file

(A "backreference" is a part of a regular expression that refers to a previous part of a regular expression. The most common backreferences are &, \1, \2, \3, ... , \9).

Some examples that demonstrate the power of this technique:

Lowercase the entire file

:%s/.*/\L&/g

(& is a handy backreference that refers to the complete text of the match.)

Uppercase all words that are preceded by a < (i.e. opening HTML tag names):

:%s/<\(\w*\)/<\U\1/g

References

Comments

Note also the gu<motion> and gU<motion> commands.

For example, ggguG will lowercase the entire file. (gg = go to top, gu = lowercase, G = go to EOF).


By using the \0 general backref instead of the name ones (\1, \2 etc) you can save some typing for on replace stanza of the regex.

This regex upper cases an explicit set of words to uppercase in a file.
:%s/\(select\)\|\(order)\|\(by\)\|\(from\)\|\(where\)/\U\0/g

Not rocket science, but otherwise you'd have to do this:
:%s/\(select\)\|\(order)\|\(by\)\|\(from\)\|\(where\)/\U\1\U\2\U\3\U\4\U\5/g

[edit:  Much easier to just use this, where either 0 or 1 will work:]
:%s/\(select\|order\|by\|from\|where\)/\U\1/g

convert HTML-Tags to uppercase

:%s/<\/\=\(\w\+\)\>/\U&/g

or to lowercase

:%s/<\/\=\(\w\+\)\>/\L&/g