47

The most simple version is: create ~/.vim/undo-dir/ directory and add the following to the .vimrc: set undodir=~/.vim/undo-dir set undofile However, it's not flawless, and it's better to read on. From my own .vimrc: " Let's save undo info! if !isdirectory($HOME."/.vim") call mkdir($HOME."/.vim", "", 0770) endif if !isdirectory($HOME."/.vim/undo-dir") ...


18

Check out vim's documentation on undo branches. u is essentially going to take you back to the parent of the current node, whereas g- allows you to traverse the current node's siblings before traversing to the root of the branch. Each node represents a change in the file. The documentation's description is a bit obscure but that is how I understand it. You ...


17

From nvi(1): u Undo the last change made to the file. If repeated, the u command alternates between these two states. The . command, when used immediately after u, causes the change log to be rolled forward or backward, depending on the action of the u command. So press u, and then keep pressing . for more undo; If you ...


16

Vim offers a limited ability to specify the scope of an undoable change with the Ctrl-G u command, which breaks the undo sequence. See :help i_CTRL-G_u In your case, the solution would be to remap <C-R> like this: :inoremap <C-R> <C-G>u<C-R> Then typing <C-O>u will undo just the changes made since you typed <C-R>. See ...


15

From the help zuw *zug* *zuw* zug Undo |zw| and |zg|, remove the word from the entry in 'spellfile'. Count used as with |zg|. zuW *zuG* *zuW* zuG Undo |zW| and |zG|, remove the word from the internal word list. Count used as with |zg|.


14

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: ...


12

For simple cases like this, you can simply press g-. Essentially, what this does is traverse the change list chronologically (treating a u as an actual change), rather than linearly (like with u/<C-r>). For example, if you've inserted a few lines, and you press g-, this happens:


12

For navigating the undo tree your best bet is to use the Gundo plugin. This creates a new split window with special bindings to jump to different branches within the undo tree as well as a preview pane that shows you exactly what changed between each node in the tree.


11

Manually breaking the undo sequence You can manually break the undo sequence in Insert mode with <C-G>u. From the help: CTRL-G u break undo sequence, start new change Example iHello<C-G>u world!<Esc>u This will leave you with the text Hello Breakdown of commands i " Enter Insert mode Hello ...


11

I think you are interested in :h undo-blocks. To make the long command, e.g. d0kJx, undoable as a single change, you can run it from the command line through normal, e.g.: :normal! d0kJx Here the ! ensures that we do not use custom mappings.


8

What I do is add the following to my .vimrc: set undofile set undodir=~/.vim/undo/ That way all your undo/change tree information is saved across vim sessions, and next time you open your file you can still undo/redo old edits.


8

I had this exact problem. Here's what I added to my vimrc to fix it for me: " Always write undo history, but only read it on demand " use <leader>u to load old undo info " modified from example in :help undo-persistence if has('persistent_undo') set undodir=~/.vim/undo " location to store undofiles nnoremap <leader>u :call ReadUndo()<CR&...


8

You can set a persistent undo with the following settings: set undodir=~/.vim/undodir set undofile This is avaible in Vim 7.3 an above


8

You can use <c-g>u in insert mode to break undo sequence and start new change. Like this: imap # <c-g>u<Esc>:w<CR># (removed the second <esc>, as it is not needed.) See :help i_CTRL-G_u


8

The change list is a different thing from the undo list. The change list is a list of locations of changes, and it can be viewed with :changes and navigated with e.g. g; and g,. The g+ and g- commands that you’re using move instead through the undo list. This is a tree of different file states: it’s what you are navigating when you undo and redo changes. The ...


7

it works for me: set mouse=a map <ScrollWheelUp> u map <ScrollWheelDown> <C-R> But i hope this is just for fun. Must be horrible =)


6

Assuming that no :w or :write command was issued, all of the changes should still be in the buffer only, not written to disk. An :earlier 30s command should restore your buffer to the state it was in 30 seconds ago. Of course, if you want to discard all changes, there's always :quit!.


6

In some cases, this might be enough: :changes Unfortunately it doesn't show a complete summary of your editing history. It appears to show only the line of text which you landed on after each change. That is good enough to display all single-line inserts or edits, but only the last line of a multi-line insert, and no deleted text. It can only be ...


6

You could try <C-w> (deletes a word to the left) or <C-u> (clear characters before cursor when pressed once, deletes linebreak when pressed a second time). See :help i_CTRL-W and :help i_CTRL-U. If you paste a lot from a register it is of course faster to undo your changes, you can use insert-normal-mode with <C-o> for that (execute one ...


6

From insert mode: If your pasted text is smaller than one line (which is what your 2nd point implies), simply hit <C-u> to remove all characters before cursor in the current line. If pasted text is more than one line, your 2nd solution would work better, adapted as follows: <C-o>u. Both points above will start and stay - or finish - in insert ...


6

New solution You can view your last changes with the :changes command. So you can fecth your most recent line change with a regex and then apply the line to matchadd() as suggested by @joeytwiddle. Here is the code : function! DiffWithPrevious() call clearmatches() redir => message silent changes redir END let line = matchstr(message, '\v\n\s{...


6

For those who have this problem and wonder how to work around it without silencing all the errors, here is how I did it in my .vimrc: au BufWritePre * try | undojoin | Neoformat | catch /^Vim\%((\a\+)\)\=:E790/ | endtry Credit goes to Christian Brabandt and to this answer from Randy Morris


5

Try to :set nocompatible and see if that does it. vi had only one undo level so Vim's ability to behave differently is one of the non-compatible changes the nocompatible setting implements.


5

Oooh, ooh, finally a chance to show off this nifty command! Vim can "go back in time." It has an :earlier command... :ea :earlier :earlier {count} Go to older text state {count} times. :earlier {N}s Go to older text state about {N} seconds before. ...


5

Unfortunately, this feature is not possible in vim right now. Although it's apparently a popular request, as many people have asked the same thing. I did find this discussion from two years ago on the vim mailing group. Bram Moolenaar (The founder, inventor, head developer, and current maintainer of vim) chimed in, and mentioned the possibility of a ...


5

I don't recall anything like that with e!. At least not since 'persistent undo' was added back in 2010 as part of v7.3(b?). (I confirmed that the command sequence discussed below was in the documentation from that point forward.) I think when the feature was first added a lot of people wouldn't have had it enabled (i.e. :set undofile). In that case e! would ...


4

Update 2015-06-28: I fixed a small bug, and released this as a plugin. The plugin code is slightly better, in that it warns again after moving the cursor; I recommend you use the plugin. The answer from superjer works great, but has the unfortunate side-effect that you can only undo changes from the last Vim session, and not all previous Vim sessions. This ...


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 ...


4

Become comfortable with vimtutor. Doesn't matter if you mess the file up, because it's a temporary version. Learn to undo something with normal mode and u. It's kinda difficult to have a catastrophe with your system, by accident from Vim. If you find yourself accidentally typing :! rm / -rf then you've got bigger problems than Vim. Don't do that at all. ...


4

The gundo plugin has a preview window, where it shows the diff between the selected version and the previous one. You can locate the relevant changes in the change tree, then go to the preview window, save the buffer to a patch file, then edit the file to keep the relevant changes, then reverse the patch. Something like diff-fold and / or patchutils can ...


Only top voted, non community-wiki answers of a minimum length are eligible