Reputation: 543
I had a file open with gvim and it asked me if I wanted to reload the current buffer because it detected a newer version of the file. I accepted, but it turns out the new buffer is not what I want. Is there a way to recover the old buffer? The file on disk (and the swp file, which I viewed with vim -r
) contains the new buffer) unfortunately.
Upvotes: 3
Views: 793
Reputation: 8258
If your buffer had fewer lines than the 'undoreload'
settings, then its content will be stored in the undotree just before it was reloaded. This means, that a simple u
or g-
should get your buffer contents back. A plugin like my histwin plugin or the famous Gundo or undotree plugin could be helpful as well.
Note, this needs a Vim of at least version 7.3 or newer to work. Older Vims didn't store the buffer state on reloading.
Upvotes: 5
Reputation: 177
I have something like this is my .vimrc
" Backup stuff
set backupdir=~/.vim/backup
set directory=~/.vim/swap
set undodir=~/.vim/undo
I can help with cases like this were you accidentally overwrite your local changes and you want to find your old version of the file. Make sure to create the directories first to avoid errors on startup.
Upvotes: 1
Reputation: 1959
Try u
(undo; if you’ve made changes since reloading, repeat until the old version shows up).
I am in terminal Vim, but I don’t think that should make a difference here. I opened a test file with Vim in one terminal tab, went to a new tab and edited it there, saved that, switched back to the first tab, ran :e
to reload the file, and then switched it back to the last version I had opened in the tab with u
.
Upvotes: 2