4

I've written a script to convert 3 and 4 characters words, since they often are abreviations in the texts I work on. Here is the starting text : Diabetes - Hbp - Copd - I want to transform it to : Diabetes - HBP - COPD - My script is composed of two lines:

%s/\v<(\w)(\w)(\w) /\u\1\u\2\u\3/g

%s/\v<(\w)(\w)(\w)(\w) /\u\1\u\2\u\3\u\4/g

Which is working, but I guess there is a more elegant solution. How to make vim match and upcase only n to m characters words?

:%s/\<\w\{3,4}\> matches 3 or 4 characters words, but I can't find how to upcase my selection ...

1 Answer 1

4

I would propose:

:%s/\<\w\{3,4}\>/\U\0/g
2
  • 1
    Thanks a lot, for the fast and accurate answer. I just found the whole match operator \0 and tried :%s/\<\w\{3,4}\>/\u\0/g but it only makes the first letter of the word uppercase ... Commented Aug 24, 2022 at 17:59
  • 1
    Your are welcome :-) you have also the \l and \L operators and as an alternative for \0 you have &. Commented Aug 24, 2022 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.