Manually breaking the undo sequence
You can manually break the undo sequence in Insert mode with
<C-G>u. From the help:
CTRL-G u break undo sequence, start new change
This will leave you with the text
Breakdown of commands
i " Enter Insert mode
Hello " Type 'Hello'
<C-G>u " Break the undo sequence
world! " Type ' world!'
<Esc> " Return to Normal mode
u " Undo
Finer-grained undo-level control
Here's a small snippet of VimScript that uses
<C-G>u to create undo breaks when you start deleting text, and also when you start entering text again after a deletion.
let l:result = a:key
let l:result = "\<C-G>u".l:result
let s:deleting = 1
let s:deleting = 0
call feedkeys("\<BS>\<C-G>u".a:char, 'n')
autocmd InsertEnter * let s:deleting = 0
autocmd InsertCharPre * call s:check_undo_break(v:char)
inoremap <expr> <BS> <SID>start_delete("\<BS>")
inoremap <expr> <C-W> <SID>start_delete("\<C-W>")
inoremap <expr> <C-U> <SID>start_delete("\<C-U>")
This will work for
<C-W> (delete a word), and
<C-U> (delete to beginning of line). If there are more ways to delete in Insert mode, they can be supported by adding additional
inoremap calls at the end.