I want to modify the first word of each line: remove underscore character and capitalize the next character.


   word_example_one other_words ...   
   word_example_two other_words ...   
   word_example_three other_words ...  

Expected output:

   wordExampleOne other_words ...  
   wordExampleTwo other_words ...  
   wordExampleThree other_words ...  

2 Answers 2


You could try substitute with :h sub-replace-special:

:%s/^\w\+/\=substitute(submatch(0), '_\(\a\)', '\u\1', 'g')

with :%s/^\w\+/ you target first word, then with special replace, you substitute within whole match (first word in our case), using :h substitute() function.

Thus, having text like:

word_example_one ...  word_example_one ...
word_example_two ...  word_example_two ...
word_example_three ...  word_example_three ...

and running this command, you get:

wordExampleOne ...  word_example_one ...
wordExampleTwo ...  word_example_two ...
wordExampleThree ...  word_example_three ...
  • Sometimes there are leading spaces at the start of each line.
    – Fisher
    Commented Jul 26, 2022 at 11:39
  • add \s* to the regex...
    – Maxim Kim
    Commented Jul 26, 2022 at 11:47
  • %s/^\s*\w\+/\=substitute(submatch(0), '_\(\a\)', '\u\1', 'g')
    – Maxim Kim
    Commented Jul 26, 2022 at 11:48
  • Add another solution to the answer? Visual select the words then :'<,'>s:\%V\v_(.):\U\1:g.
    – Fisher
    Commented Jul 26, 2022 at 11:57
  • it wouldn't work for all cases -- words might be different length and you might not be able to select those words without selecting unneeded (non-first). So you might replace not what you wanted. i.imgur.com/ee6y7rM.png
    – Maxim Kim
    Commented Jul 26, 2022 at 12:07

Another :substitute-based option, where the pattern is simpler:


and then use & to repeat this on the line until you are satisfied (it only changes the first _ in the line each time). If you want to repeat this across the whole file, use :%substitute first and then type g& as many times as necessary.

With tpope's vim-abolish, crc coerces to camelCase. So you could do (for example)

:[range]normal ^crc

to change the first word on the lines covered by [range] to camel case.

  • :substitute/\v_(\a)/\u\1 will replace all _\a. OP wants to replace only in the first word. (I answered this solution and deleted upon OP's clarification. And I'm jealous of your 1 upvote. xD)
    – 3N4N
    Commented Jul 28, 2022 at 7:25
  • @kadekai not so: the g flag is missing, so the substitute only happens for the first occurence in the line(s) covered by the range (default current line). Hence the need to press & to continue until the word is changed; it’s more work, but I find the pattern and replacement easier to type
    – D. Ben Knoble
    Commented Jul 28, 2022 at 12:15
  • That makes sense. Specially if you use macros. I myself would do it that way.
    – 3N4N
    Commented Jul 28, 2022 at 12:38

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.