2

I started to learn ViM one month ago, so I'm not a ViM expert.

My question is: if I make some changes in insert mode (let's say I type some text, then I delete the last two words and finally type some other words) and then I press <ESC> u the entire text I typed in Insert mode disappears. I know the existence of the <CTRL-G> u command, which breaks the undo history, but I find it annoying typing <CTRL-G> u every single time I delete something. Is there a way to automate the execution of that command every time I press a key (<SPACE>, <BS>, ...) in insert mode?

Thanks a lot.

3

You can define insert mode mappings for those keys:

:inoremap <space> <c-g>u<space>
:inoremap <bs>    <c-g><bs>
:inoremap <c-w>   <c-g><c-w>
" etc.
2

See :help ins-special-special, that section has some specific examples on using inoremap to trigger <C-G>u automatically for specific keystrokes such as backspace or newline.

An example for using CTRL-G u:

    :inoremap <C-H> <C-G>u<C-H>

This redefines the backspace key to start a new undo sequence. You can now undo the effect of the backspace key, without changing what you typed before that, with CTRL-O u. Another example:

    :inoremap <CR> <C-]><C-G>u<CR>

This breaks undo at each line break. It also expands abbreviations before this.

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.