61
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 ...
- 1,885
23
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 ...
- 23k
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 ...
- 476
19
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 &...
- 6,104
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 ...
- 59.2k
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 ...
- 2,587
15
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/<...
- 14.2k
14
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 ...
- 1,057
13
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&...
- 7,370
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 ! ...
- 9,085
11
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
- 1,076
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 ...
- 2,331
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 ...
- 29.9k
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 ...
- 2,039
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 ...
- 641
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.)
...
- 8,817
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 ...
- 9,029
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 ...
- 9,440
7
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\+)...
- 171
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 =)
- 1,076
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 ...
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 ...
- 18.9k
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.
...
- 15.5k
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.
- 1,456
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 ...
- 3,512
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
...
- 29.9k
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 ...
- 3,965
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
:...
- 14.2k
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 ...
- 17.1k
5
votes
Accepted
undo last change on current line
This will probably do what you want:
function! Undoline()
let pos = getpos(".")
let current = getline(pos[1])
let chg = changenr()
while changenr() > 0 && current ==# getline(pos[1]...
- 2,768
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
undo-redo × 80key-bindings × 8
persistent-state × 5
vimrc × 4
normal-mode × 4
insert-mode × 4
repeated-commands × 4
git × 4
command-line × 3
vimdiff × 3
neovim × 2
buffers × 2
cursor-movement × 2
vim-windows × 2
save × 2
scrolling × 2
filesystem × 2
command-history × 2
vimscript × 1
cut-copy-paste × 1
autocmd × 1
autocompletion × 1
microsoft-windows × 1
cursor-motions × 1
highlight × 1