I am using the following regex to find the first letter of the last word on each line in a file:
I want to now lower case this match on every line in a file. How do I do it?
Before: Insomnia Cures
After : Insomnia cures
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
For this specific task, an alternative approach is not to use a
:substitute command at all!
:%norm!$Bgul % # On every line, norm! # Run the normal mode commands: $ # Move to the end of the line, B # Move to the start of the last WORD on the line, and gu # Convert to lower case... l # The first letter of the word.
You could do the following:
\S\+ match any non space characters (a word)
Since its is followed by
\s*$, that only match trailing spaces at the end of the line, this word must be at the end of the line.
\l transforms the match by changing the first letter to lowercase.
You can find more information at:
:help :s% for the transformation possibilities.
You can find also more information about patterns at