using "u" and CTRL-R will not get you to all possible text states
while repeating "g-" and "g+" does.
==============================================================================
5. Undo persistence *undo-persistence* *persistent-undo*
When unloading a buffer Vim normally destroys the tree of undos created for
that buffer. By setting the 'undofile' option, Vim will automatically save
your undo history when you write a file and restore undo history when you edit
the file again.
The 'undofile' option is checked after writing a file, before the BufWritePost
autocommands. If you want to control what files to write undo information
for, you can use a BufWritePre autocommand: >
au BufWritePre /tmp/* setlocal noundofile
Vim saves undo trees in a separate undo file, one for each edited file, using
a simple scheme that maps filesystem paths directly to undo files. Vim will
detect if an undo file is no longer synchronized with the file it was written
for (with a hash of the file contents) and ignore it when the file was changed
after the undo file was written, to prevent corruption. An undo file is also
ignored if its owner differs from the owner of the edited file, except when
the owner of the undo file is the current user. Set 'verbose' to get a
message about that when opening a file.
Location of the undo files is controlled by the 'undodir' option, by default
they are saved to the dedicated directory in the application data folder.
You can also save and restore undo histories by using ":wundo" and ":rundo"
respectively:
*:wundo* *:rundo*
:wundo[!] {file}
Write undo history to {file}.
When {file} exists and it does not look like an undo file
(the magic number at the start of the file is wrong), then
this fails, unless the ! was added.
If it exists and does look like an undo file it is
overwritten. If there is no undo-history, nothing will be
written.
Implementation detail: Overwriting happens by first deleting
the existing file and then creating a new file with the same
name. So it is not possible to overwrite an existing undofile
in a write-protected directory.
:rundo {file} Read undo history from {file}.
You can use these in autocommands to explicitly specify the name of the
history file. E.g.: >
au BufReadPost * call ReadUndo()
au BufWritePost * call WriteUndo()
func ReadUndo()
if filereadable(expand('%:h') .. '/UNDO/' .. expand('%:t'))
rundo %:h/UNDO/%:t
endif
endfunc
func WriteUndo()
let dirname = expand('%:h') .. '/UNDO'
if !isdirectory(dirname)
call mkdir(dirname)
endif
wundo %:h/UNDO/%:t
endfunc
You should keep 'undofile' off, otherwise you end up with two undo files for
every write.
You can use the |undofile()| function to find out the file name that Vim would
use.
Note that while reading/writing files and 'undofile' is set most errors will
be silent, unless 'verbose' is set. With :wundo and :rundo you will get more
error messages, e.g., when the file cannot be read or written.
NOTE: undo files are never deleted by Vim. You need to delete them yourself.
Reading an existing undo file may fail for several reasons:
*E822* It cannot be opened, because the file permissions don't allow it.
*E823* The magic number at the start of the file doesn't match. This usually
means it is not an undo file.
*E824* The version number of the undo file indicates that it's written by a
newer version of Vim. You need that newer version to open it. Don't
write the buffer if you want to keep the undo info in the file.
"File contents changed, cannot use undo info"
The file text differs from when the undo file was written. This means
the undo file cannot be used, it would corrupt the text. This also
happens when 'encoding' differs from when the undo file was written.
*E825* The undo file does not contain valid contents and cannot be used.
"Not reading undo file, owner differs"
The undo file is owned by someone else than the owner of the text
file. For