11

Too often do I make the mistake of typing with the wrong window focused (and have done so a few times with a USB authenticator). In programs with lots of hotkeys enabled (Gmail, VLC, Vim), this often has the unintended consequence of making unwanted modifications.

I can sometimes trace my steps and remember what I have typed to repair any changes or revert any settings that I tweaked if I catch myself early. Unfortunately, that is not always the case.

In Vim, I could have something as innocuous and apparent as altering a line of text which is easily undone. Or, I could have unknowingly modified something not so easily identified. If I accidentally type a sequence of unwanted commands in Vim, how can I check for and undo any edits or changes to settings?

  • 1
    Are you looking for an "revert" action? :e! would do that – Jasper Feb 3 '15 at 19:48
  • 1
    I think :e! would be bad if you haven't saved since the last correct state, and u might be insufficient if you typed a bunch of stuff. (If you typed date month, for example, then you would have deleted a tag, set a mark, and performed a search, which would be quite confusing to undo via u.) – wchargin Feb 3 '15 at 20:19
  • 1
    @WChargin Not speaking about a u being part of the sequence, performing some undos in the middle. – yo' Feb 3 '15 at 20:28
13

Invoke

:undolist

to see when you made the last set of changes. If it says that there were a few changes made, e.g., "3 seconds ago," and you know that you only intentionally changed it, say, a minute ago, then execute

:earlier 10s

to revert to the desired state. Otherwise, if there are no recent changes, you don't have to do anything.


Further reference: :help undo-tree.

4

To rephrase the question, you are looking for a way to see or otherwise get notified about the changes you have made to a file, and then use an undo feature. There maybe other way or even plugins which supports this. But I'll explain what I've been using for the same purpose.

You combine existing vim feature to get this done. For example, you could define a function such as:

" Function: View changes after the last save
function! s:DiffSinceSave()
  let filetype=&ft
  diffthis
  vnew | r # | normal! 1Gdd
  diffthis
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
  exe "normal! ]c"
endfunction
com! DiffSaved call s:DiffSinceSave()

You can then call it later and even map a shortcut key.

" Map key to function: View changes after the last save
nnoremap <leader>? :DiffSaved<CR>

This will open a split window based diff view which will show you changes made since the last save. After you have identified the changes made, then you can quit that view and use other vim features such as undo and undo tree, etc. or even plugins to revert back to previous states.

I realize that this is not ideal because you have to have a certain kind of workflow for this to be useful. But this works fairly well. And on the plus side, we aren't changing how vim works or relying on anything outside.

I hope you find this useful. I adapted this solution a while ago from snippets found on the Internet including vim Wikia. So that's where any credit is due.

3

It seems to me the simple answer to this is to press u until you undo a change you wanted to keep, then press Ctrl-R.

0

Like @superjer, I keep pressing u until the red [+] marker in my statusline disappears.

set statusline=%f\ %#Error#%m%##
  • It might be nice to have a key to undo automatically back to the last unmodified state. But :e! is also a nice solution; these days Vim will even keep your "discarded" changes in the undo history. – joeytwiddle Aug 11 '15 at 6:52
  • 5
    That command is :earlier 1f. The 1f means "1 previous saved state", so it will go back to the previous time you saved the file. If the buffer is modified, then that undoes all the modifications. If the buffer isn't modified (i.e., it is currently in a saved state), then it will go to the previous time you saved the file. – jamessan Aug 11 '15 at 14:34

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.