When I have to type a lot of text I often tend to keep my finger pressed on shift when I write the first letter of a sentence which often give:
[...]end of sentence. NEw sentence[...]
Here the E
of NEw
should be lower case. I am then trying to create a function which would detect if the second letter of the first word of the sentence I'm typing is upper case and which would lower case it. The important part would be that the correction should be done automatically while I'm typing the end of my sentence.
So far I've tried to play with the autocommand event InsertCharPre
before realizing that the text can't be modified by a function triggered by this event.
What would be a good solution?
Note That so far I don't need to focus on edges cases like acronyms which should be in upper case or this kind of things.
EDIT I've made this, which is a not-perfect workaround:
autocmd CursorMovedI * call RemoveUnwantedUpper()
function! RemoveUnwantedUpper()
" Get the current sentence
" Based on http://stackoverflow.com/a/23315227/4194289
let l:save_clipboard = &clipboard
set clipboard= " Avoid clobbering the selection and clipboard registers.
let l:save_reg = getreg('"')
let l:save_regmode = getregtype('"')
normal! y(
normal! ``
let l:sentence =getreg('"')
call setreg('"', l:save_reg, l:save_regmode)
let &clipboard = l:save_clipboard
" Check that we entered a new word (space inserted)
if l:sentence[len(l:sentence)-1] != " "
return
endif
" Check if the word is the first one of the sentence
let l:size = len(split(l:sentence, " "))
if l:size > 1
return
endif
" If the last char entered is a space (new word) remove the unwanted Upper case
normal! bl
normal! vu
normal! ``
endfunction
It has a problem since the first character I enter in insert mode is moved to the end of the line, but I think that can be corrected.
I guess now my question becomes a code review question:
- How can I get rid of the side effect which moves the first character inserted?
- Is it the best method possible?
- This method seems to slow down Vim: how could it be improved?