Vim Tips Wiki
Advertisement

If you call the :e or :edit command with no arguments, Vim will load the current file into the buffer from the disk, refreshing the contents if the file has changed. One real problem with this, is that the reload causes the undo history to be cleared. You can't undo back past the edit to the text you were editing earlier.

This is a little mapping you can add to your .vimrc which will change the :e command to reload the current file in a different way, preserving your undo history:

" :e usually clears undo history, so we don't really do :e any more.

" We delete the contents of the buffer, then read the file in, which

" is an operation we can undo. We must delete the top (empty) line also.


map :e<Enter> :%d<Enter>:r<Enter>:0<Enter>dd


" BUG: vim still thinks the file is out of sync with the buffer, so if you

" quit without writing the file, vim complains, which is not how :e behaved.

Note I haven't mapped the :edit command, only :e. Also it won't work if you use :e with an argument. Please do improve this version.

Advertisement