58 votes

How can I use the undofile?

The simplest 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 ...
user avatar
  • 1,835
20 votes
Accepted

Difference between g- and u

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 ...
user avatar
20 votes
Accepted

How can I undo `zg`?

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 ...
user avatar
  • 21.5k
19 votes
Accepted

Can I undo multiple times in nvi and/or the original vi?

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 ...
user avatar
18 votes
Accepted

Undo in insert mode

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 &...
user avatar
  • 5,934
16 votes
Accepted

How can I safely undo changes that I may or may not have made?

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 ...
user avatar
  • 2,527
13 votes
Accepted

How do you use changes tree in vim

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 ...
user avatar
12 votes

How to change undo granularity in Vim?

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&...
user avatar
  • 7,240
12 votes

How do you use changes tree in vim

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/<...
user avatar
  • 13.8k
11 votes
Accepted

How to keep in the undo history just one change for this command?

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 ! ...
user avatar
10 votes
Accepted

How to cache file editing history in Vim, that it would not disappear after switching the file?

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 ...
user avatar
  • 2,251
10 votes

Any way to retrieve lost undo tree after closing window (but buffer is still open)

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
user avatar
10 votes

Why does Vim say "Already at newest change" despite a full "changes" history list?

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 ...
user avatar
  • 28.2k
9 votes

Undo in insert mode

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 ...
user avatar
  • 1,999
8 votes

Can I be notified when I'm undoing changes from the undofile?

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 ...
user avatar
  • 631
8 votes
Accepted

How to make insert mode mapping count as multiple undos?

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.) ...
user avatar
  • 8,665
7 votes

What can happen if I accidentally paste into command mode? What should I do if I do this?

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 ...
user avatar
  • 8,819
7 votes

Undo in insert mode

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 ...
user avatar
  • 9,340
7 votes
Accepted

Console vim(x): Can I map the scroll wheel to undo--redo?

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 =)
user avatar
7 votes
Accepted

Using colon-commands inside ternary expression

inoremap (g == 0 ? :undo : :redo) You are using an expression mapping, so you must return an expression, e.g. in your case a string describing the command you want Vim to perform. That means your ...
user avatar
7 votes
Accepted

How to drop undo history?

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 ...
user avatar
  • 18.4k
6 votes
Accepted

Undo multiple changes in Cygwin's vim

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.
user avatar
6 votes
Accepted

Highlighted undo in Vim

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. ...
user avatar
  • 15.2k
6 votes

Search all versions of a file in the undo tree

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 ...
user avatar
  • 3,462
6 votes

Turn Vim's multiple undo history into git commits?

Here's a quick and dirty function that does what you request: function! UndoCommits(steps) for i in range(a:steps) undo endfor for i in range(a:steps) write ...
user avatar
  • 28.2k
6 votes

Is it possible to remove an entry from the undo history?

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\+)...
user avatar
  • 161
6 votes
Accepted

What does undo undo?

It undo the last command (e.g. i, c, d). The insert (i) command can indeed be either: rather large if you type a long text without leaving the insert mode or rather short if you just make a small ...
user avatar
5 votes

Can I be notified when I'm undoing changes from the undofile?

Oooh, ooh, finally a chance to show off this nifty command! Vim can "go back in time." It has an :earlier command... :ea :earlier :...
user avatar
  • 13.8k
5 votes
Accepted

Redo earlier edit operation

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 ...
user avatar
  • 16.8k
5 votes
Accepted

Possible to rename file and maintain undo?

Using :saveas keeps the undo history intact. :saveas is equivalent to the combination of :w newname followed by :e #. I usually find it more handy than the :w + :e, especially since with :saveas it's ...
user avatar
  • 26.3k

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