简体   繁体   中英

what is the definition of a “change” in vim in terms of undo?

I'm trying to understand multi-level undo in vim. I opened vim and typed:

1
2
3
4
5
6
7

and then I typed:

:u 1

I got message:

0 changes; before #1  22:53:11

and when I typed:

:u 2

I saw:

E830: Undo number 2 not found

my understanding is every vi command or character typed in edit mode is counted as a "change" but obvious that is not the case.

Could any seasoned vim expert clarify?

In general, a single change is every normal mode command that changes your buffer.

For insert mode, everything typed is considered a single change, until you leave insert mode. There are exceptions however. Using the cursor keys breaks the undo sequence. Another exception is, if you press Ctrl+g u which will intentionally break the undo sequence. Also when temporarily leaving insert mode using eg Ctrl+o this will break a change.

You can modify undo behavior like this (in your ~/.vimrc ):

inoremap <BS> <c-g>u<BS>
inoremap <CR> <c-g>u<CR>
inoremap <del> <c-g>u<del>
inoremap <c-w> <c-g>u<c-w>

For more information see: :h i_Ctrl-g_u and read this link on vim wiki .

If you are using some completion plugin like me, you have to make more than that. I am using deoplete and in this case I had to do this:

" <CR>: close popup and save indent.
" Now each Enter creates a undo point ":h i_Ctrl-g_u"
inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
  return deoplete#mappings#smart_close_popup() . "\<C-g>u\<CR>"
endfunction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM