I would like to know the proper substitution command in vi that would accomplish this.

  • Did you meant capitalizying the first word or first character of the first word? – SibiCoder Sep 24 '16 at 14:59

This is a normal vim substitute command with ; as separation character; you can use other characters beside the default / as separation character, as long as it does not appear in the expression.

%s        : Do substitute command on the entire buffer

[.?!]     : match every . or ? or ! and
\_s*      : match every whitespace including newline after it if there is any, and
\zs       : start the match from here and
\<\w\+\>  : select first word

\U&       : then make it capitalize (or use \u& to only capitalize first character)

g         : globally
| improve this answer | |
  • What do the semi colons represent? – John Bradowski Sep 23 '16 at 0:44
  • 4
    @JohnBradowski, one can use other delimiters like ; # instead of usual / – Sundeep Sep 23 '16 at 3:57
  • 2
    There is often a space between a punctuation mark and the following word, so your command could be %s;[.?!]\s\?\<\w\+\>;\U&;g to handle these white-spaces. – statox Sep 23 '16 at 9:05
  • 1
    @statox To be useful, you'd presumably also want the regexp to handle newlines, paragraph breaks, etc. – Rich Sep 23 '16 at 9:29
  • @Sundeep, tanx for explanation. answer edited. – dNitro Sep 23 '16 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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