Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a string matched by certain regexp with its length in Vim

Tags:

regex

vim

In Vim, how do I replace a string matched by certain regexp with its length, like this:

Regexp = "\w*"

"hello" -> "5"
"bye" -> "3"

Is it possible to do it with :s or do I need to write some script?

Best regards

like image 686
Thanh DK Avatar asked Apr 07 '11 09:04

Thanh DK


1 Answers

Replace 'hello' with your regex expression and this should work in VIM:

:s/hello/\=strlen(submatch(0))/ 
like image 189
Jake Avatar answered Oct 06 '22 19:10

Jake