When writing in MS Word, or in Open Office, the first letter of each sentence is capitalized automatically by the editor, without needing to press the Shift key. This makes typing much easier.

Is there a script, mode or something that will cause Vim to likewise capitalize the first letter of all sentences automatically, as you type?

  • 1
    This might be useful if you mostly edit plain text in Vim. For programming languages, this probably isn't that useful. I personally hate this feature, and turn it off in Office. Apr 3, 2014 at 7:35
  • @IngoKarkat Yes, I mostly edit plain text in Vim, as you guessed :)
    – sashoalm
    Apr 3, 2014 at 9:04

2 Answers 2


This can be done with :help :map-expr mappings for all lowercase characters that check for a preceding end of a sentence. If there's a sentence-ending character before the cursor, it returns the uppercase character, else the typed lowercase character.

You can force a lowercase character after a sentence-ending character by pressing Shift while typing the character. I.e., in effect case is "toggled" for the first letter.

I use a loop to build the individual mappings:

for char in split('abcdefghijklmnopqrstuvwxyz', '\zs')
    exe printf("inoremap <expr> %s search('[.!?]\\_s\\+\\%%#', 'bcnw') ? '%s' : '%s'", char, toupper(char), char)

for char in split('ABCDEFGHIJKLMNOPQRSTUVWXYZ', '\zs')
    exe printf("inoremap <expr> %s search('[.!?]\\_s\\+\\%%#', 'bcnW') ? '%s' : '%s'", char, tolower(char), char)
  • Do I put that in .vimrc? Also, I need it to work for Cyrillic characters as well, does vim have an isalpha function like in C++? I'm asking because I see you using toupper.
    – sashoalm
    Apr 3, 2014 at 9:07
  • Yes, that could be placed into ~/.vimrc, or as a separate plugin in ~/.vim/plugin/name.vim. To check for alphabetic characters, try the [[:alpha:]] regexp collection. Apr 3, 2014 at 9:12
  • Ah yes, that's a nice addition, thanks! Also, the check for "end of sentence" can be tweaked to your needs (it's the [.!?]\_s\+ regexp part). Apr 3, 2014 at 10:35
  • The only problem is this script seems to add a noticeable lag when typing, I don't know if it's searching too much text for each symbol?
    – sashoalm
    Apr 3, 2014 at 11:22
  • You can try limiting the range with an additional line('.') - 1 argument to search(). Apr 3, 2014 at 11:24

I think for this I would use an InsertCharPre autocmd to check for the beginning of a sentence, and convert v:char to uppercase if found.

I stole the regex for finding a end of sentence from Ingo's answer, and tweaked it to also work on the very first line of the file. This is what I came up with:

  autocmd InsertCharPre * if search('\v(%^|[.!?]\_s)\_s*%#', 'bcnw') != 0 | let v:char = toupper(v:char) | endif
augroup END

Note, you can manually bypass the autocmd (or a mapping) to insert a lowercase character, if you type CTRL+V before entering the character to insert it literally. Unless of course you've remapped CTRL+V to something else, like paste. Then you should be able to use CTRL+Q instead.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .