CCC
CCC

Reputation: 2242

Can I recover my code lost during my last change in vim?

I am very sad I deleted wrong function without commit to SVN server by using vim After I compiled it I found I made the mistake. I 'make' the file also via vim.

Now I haven't closed the file and it has .swp file. I tried to use 'u' command to restore my deletion but failed. vim said it's the latest changes. sigh.... Anyway I can restore my function?

Million thanks.

Upvotes: 14

Views: 41901

Answers (4)

alpha_989
alpha_989

Reputation: 5350

There are a couple of ways to recover text that you may have unwittingly lost due to a crash or because you closed your program unintentionally.

  1. Use persistent-undo. Persistent undo provides almost all the features provided by swap/backup file option in points #2 and #3, along with some other options such as granular history traversal.

    a. Set persistent-undo on:

    Put this in your .vimrc:

    set undofile set undodir=~/.vim/undodir

    b. Create the undodir

    mkdir ~/.vim/undodir

    c. To undo a change, use either of the following options

    1) g+ in normal mode to go forward in history

    2) g- in normal mode to go backward in history

    3). :earlier 20s to go back in time by 20s or earlier 10m to go back in time by 10min etc

    4) :later 20s to go forward in time by 20s or later 10m to go forward in time by 10min etc

    5). Use :undolist to get a list of undo changes

    d. To get a visualization of your undo-branches, you can use plugins like gundo.vim: http://bitbucket.org/sjl/gundo.vim/ to visualize the branches

  2. Use backup files

    a. Use this in your .vimrc set backup

    b. Set up the backup directory by putting this in your .vimrc set backupdir=~/tmp/

    c. Set up the backup file name to be added to the backup file by setting this in your .vimrc set backupext=string

  3. Use swap files

    a. Use this in your .vimrc set swapfile

    b. Set up the swap directory by putting this in your .vimrc. This may not be a good idea, because it will prevent you from having two files with the same names, or cause conflicts in swap file names.

    set directory=~/tmp/

    A better option is to provide multiple paths, so if vim encounters a conflict it can then save it to the directory that it can write to using

    set directory=,~/tmp/

    In this case, it will try to write the swap file to the current directory. If it can't, then it will attempt to write it to the ~/tmp directory

    c. Set up the backup file name to be added to the backup file by setting this in your .vimrc

    set backupext=string

TL;DR Use persistent-undo. It provides almost all features of swap and backup, and provides additional features such as granular undo which is not provided by backup and swap file options.

References 1. ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf

Upvotes: 7

sehe
sehe

Reputation: 393769

To make Drasils pointer a lot more explicit:

 :undolist

g- to 'go back in time'

g+ to 'go forward in time'

Vim 7.3+ has undo 'branches': meaning that it will save state snapshots, even if linear history was overwritten (so it isn't reachable by simple u and )

Upvotes: 17

Drasill
Drasill

Reputation: 4016

I don't know if you can recover something here, but for the future, if you user vim 7.3, you should active these options I explain in my previous comment.

I must say that the savevers plugin has saved me a lot of hours ;-)

Upvotes: 2

Andrew Aylett
Andrew Aylett

Reputation: 40760

Vim usually saves the previous version of any file edited as a backup with a ~ appended -- you could check to see whether that file is there and if so, whether it's got the right contents.

Upvotes: 3

Related Questions