Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vimscript: How to capitalize a specific character of matched text in a substitution?

Tags:

regex

vim

I'm having trouble getting this to work. I'm writing a Vim snippet that will take the current file's name and expand it into a class name, e.g.

If the current file name is: some_awesome_class.foo ... the snippet would expand to class SomeAwesomeClass

So far, I have:

substitute(Filename(), '\(^.\|_\a\)', '\u&', 'g')

.. but this only capitalizes the first letter, even though it correctly matches all the underscores. I guess what I'm looking for is a way to use the underscore as part of the search, but not have it be matched in the ampersand, i.e: it looks for _\a but only matches \a.

Any help would be much appreciated!

like image 758
Tres Avatar asked Dec 19 '25 17:12

Tres


2 Answers

Here is a suggestion:

s/\v(^|_)(\a)/\u\2/g
like image 148
sidyll Avatar answered Dec 21 '25 06:12

sidyll


You should exclude the beginning of the line and underscore from the match with \zs.

^.\|_\zs\a

This works, over here.

See :help \zs and :help \ze.

like image 30
romainl Avatar answered Dec 21 '25 08:12

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!