I am using the following regex to find the first letter of the last word on each line in a file:
%s/.* .\zs/
I want to now lower case this match on every line in a file. How do I do it?
For eg.
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 communityThis worked for me
%s/.*\zs \([A-Z]\)/\L&/
If you want to consider lines like
Insomnia (Cures)
you can use %s/.*\zs [^A-Z]\?\([A-Z]\)/\L&/
To manipulate strings at the beginning of the line you can use
%s/^\([^a-z][A-Z]\)/\L&/
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/\S\+\s*$/\l&/
The \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.
The \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 :help pattern-overview
.