37

If I'm editing two files with vim, changing to the other file ( :bnext, :bprev ) seems to drop the undo history from the open file - hitting the 'u' key reports "Already at oldest change".

For example:

  1. vim testfile1 testfile2
  2. add some stuff to testfile1
  3. :w
  4. :bn
  5. :bp
  6. u
  7. eep! can't undo!

Is there any way to keep this history for non-visible buffers?

Jeremy Kerr
  • 27,199
  • should this be in superuser or stackoverflow instead? – Ressu Jul 29 '10 at 08:32
  • @Ressu - good point, how do we tell? Meta question posted: http://meta.ubuntu.stackexchange.com/questions/47/how-do-we-tell-if-a-question-belongs-here-or-rather-at-stackoverflow-superuser – Jeremy Kerr Jul 29 '10 at 08:45
  • Depending on the outcome of that meta question, I'm likely to delete this. – Jeremy Kerr Jul 29 '10 at 09:24

3 Answers3

48

The newest version of vim (7.3) has persistent undo, so that you can make a change, close vim completely (even shutdown and restart), restart vim, and undo. In your .vimrc:

" tell it to use an undo file
set undofile
" set a directory to store the undo history
set undodir=/home/yourname/.vimundo/
tshepang
  • 1,967
frabjous
  • 6,431
  • 12
    Note that you must create the directory first, vim does not do that automatically. Undo files are saved using the filesystem path (at the same time when saving the file). If you are in /home/peter, then editing .bashrc will create the file /home/peter/.vimundo/%home%peter%.bashrc. – Lekensteyn Oct 11 '13 at 17:53
22

Looks like this will do it:

:set hidden

(in .vimrc)

Jeremy Kerr
  • 27,199
1

You can use Viewports.
"vim -o testfile1 testfile2" - open files in splitted window.
":sp filename" - split and open "filename".
":vsp filename" - vertical split and open "filename".
"Ctrl+w+arrow" - Change viewport.

yevhene
  • 1,546
  • I'd rather not keep a permanently-visible buffer for each file; it's not unusual to have >20 files open at once. – Jeremy Kerr Jul 29 '10 at 08:44