01 May, 2009

Vim subsitute with expression expansion

Just read a really interesting Vim tips and tricks post on Vim substitute with expression. In a nutshell, Vim substitute command allow you to use Vim expression, whose used in Vim script. For example, the following command will change all 'CONFIG' to 'config'.

:%s,CONFIG,\=tolower(submatch(0)),

In this example, the key is '\=' notation, which is a switch to enter expression evaluation mode. The function submatch(0) refers to the full match of the substitute command, and both tolower() and submatch() is normal Vim functions only.

Read the Vi and Vim Editor: 12 Powerful Find and Replace Examples for more examples.

No comments: