Restore Vim Backups - vim

Vim's file backup system just saved my proverbial #$$ but I have a question.
I have vim saving backups to ~/.vim/backups
To restore them I went to the directory and (sorted by date) copied the files I needed back to the necessary directories in my project folder. Easy enough, there were only 5 files. However, I'm surprised there's no obvious way to find which directory each file came from. I tried using vim -r path/to/file but that seems to use the swap and not the backup file. Since in my case vim didn't crash (I just mistakenly overwrote the files) there is no swap for these files.
So the main question is: What is the best way to restore vim backup files?
Side question: What happens in the .vim/backup/ directory when I have two same-name files from different paths (e.g. /path/one/file.html and /path/two/file.html)?

To answer the question about restoring, Vim doesn't know anything about the backups. It's backup system is very simple -- write a file. To restore, you have to do it manually.
However, you can add more info to your backups to make it easier to restore.
Ideally, the backupdir would honor trailing // to force it to expand the path of the file and replace / with %. e.g. /tmp/foo.rb would become the backup file `tmp%foo.rb'.
See :help undodir and :help directory for the undo and *.swp directory settings which do take //.
But we can fake this!
" Prevent backups from overwriting each other. The naming is weird,
" since I'm using the 'backupext' variable to append the path.
" So the file '/home/docwhat/.vimrc' becomes '.vimrc%home%docwhat~'
au BufWritePre * let &backupext ='#'.substitute(substitute(substitute(expand('%:p:h'), '/', '%', 'g'), '\', '%', 'g'), ':', '', 'g').'~'
The backup files read filename-then-directory but it does work and should be sufficiently unique.
Example: .vimrc%home%docwhat~ is as backup of /home/docwhat/.vimrc
If you wanted minute by minute backups, so you loose less work, you can append a timestamp to the file name:
au BufWritePre * let &backupext = substitute(expand('%:p:h'), '/', '%', 'g') . '%' . strftime('%FT%T') . '~'
But I'd recommend you setup a cron job to clean this directory regularly. If you edit as many files of me, this file would become huge quickly.
Alternatively, you could do something clever and change the backupdir before writing (with the BufWritePre hook), but that level of cleverness with vim is beyond me (at the moment).
Ciao!

From what I can tell, Vim doesn't save any information at all regarding the file's original location.
Saving a backup of two same-name files could either overwrite the existing backup or use a different name, depending on what's set in your .vimrc. The option 'backup' will overwrite an existing backup file, but 'writebackup' will re-name a new backup in order to avoid an overwrite.
Check out Vim's documentation for more info - :help backup

Related

Recover a vim file from the .un~ file without the undo command

How can I restore a vim file from the undo file without hitting undo?
I had a vim file that I saved while adding text. Then I ran a python command that emptied the file's contents, and I can see some of the words the file contained in the file's .un~ file. When I try to undo in the file, it says Already at latest change. I can't find the swap file in my swap files directory.
As the other answers have noted, you can't recover the whole file from vim's undo files, simply because vim seems to only keep diffs in the undo files, not the whole contents. (That makes a lot of sense, as it's space efficient.)
One thing you can try though, is to extract what's possible from your undo file:
$ strings <undo-file>
The output will not be pretty, but you could end up finding something that's valuable to you.
You can't. The undo information is linked to Vim's last knowledge of the file's contents; when they don't correspond any more, Vim cannot re-apply the changes. This is documented at :help undo-persistence:
Vim will detect if an undo file is no longer synchronized with the file it was
written for (with a hash of the file contents) and ignore it when the file was
changed after the undo file was written, to prevent corruption.
Best you can do is try to manually salvage recognizable bits in the undo file, e.g. with a hex editor, or Vim's binary mode.
It is not exactly possible, as the undo file only contains the text that was changed in a single change. If you at some point reloaded the file, the undofile should contain the complete buffer for that and starting from there one could theorectically recover the file (by going through the undo states).
I have written about this before at the vim_use mailinglist here and here (which even contains a patch, that let's you force reading in the undo-file)
You could try to patch vim and see if you can recover at least some data.
A reminder that if you have set in your .vimrc file
set backupdir=$HOME/tmp
You may have temp copies of the files that are readable and that can be renamed

Include a directory recursively for vim autocompletion

I use vim from the base directory of my source code. I would like to have autocomplete consider every word of every file in this directory (and subdirectories) when editing a single file.
Completion is controlled by the 'complete' option. With the k flag, you can have Vim scan files. The ** wildcard stands for a recursive descent into subdirectories. Voila:
:set complete=k**/*
When you want to keep the other default locations (other buffers, included files, etc.), use :set complete+= to add to it.
Alternatively, as this can be too slow for a default, you can use the 'dictionary' option and use Ctrl-X Ctrl-K completion:
:set dictionary=**/*
:argadd **/*
This will recursively load all files into buffers, and the completion will consider them. Beware that this will load all files, including files you might not want to add, such as binary files or files in CVS directories. A more fine-grained glob such as the one below might be a good idea.
:argadd **/*.cpp
I think a much better approach is to generate a tags file in your directory system using Exuberant CTags.
For starters, build your tags file in the root of your source with:
ctags -R .
This may find more than you like, but you can tune it with further command line options.
Set your tags option to refer to this file ("tags" is the default name), or use set tags=./tags;/ to search up your directory tree to the first tags file found. See :help file-searching to understand the ;/ syntax for upward search.
Finally, make sure that 't' is in your complete option. It's there by default, but check with set complete?. If it's not there, set complete+=t will put it there.
I personally remove 'i' and 'd' from my 'complete' option, because the disk access is annoying, even though typing CTRL_Y (yes, accept completion) or CTRL_N (no, reject completion) or continuing normal typing will stop the search. If all the included files are in your tags file, you'll find the completion instantly.

vim - don't save backups if in svn working copy?

I like that vim automatically saves backup files, but it is annoying when I use vim to edit a file that is in a svn working copy, since the files are already "backed up", and it creates clutter.
How do I configure vim to only save backups when I am editing a file that is NOT in a svn working copy?
Not an answer to your specific question, but I believe a better solution. Why not backup all your files into a separate directory, regardless of whether they are in source control?
Here's how, from my .vimrc. This creates the backups in the common ~/.vim_backups directory:
" Use a common directory for backups and swp files
" Create it if it doesn't exist
silent execute '!mkdir -p ~/.vim_backups'
set backupdir=~/.vim_backups//
set directory=~/.vim_backups//
See backupdir option in vim (type :help backupdir) to create backup files in different location. You can put the definition of "backupdir" to your ~/.bashrc.
One additional point - you may still want backup files even if the file is controlled by svn, since backup will contain your local changes (not checked ones)

Disabling swap files creation in vim

Is there a way to disable .swp files creation in vim? or at least create them all in one place so I can find and delete them easily.
I find them especially annoying when I copy the parent directory while editing at the same time. Of course I know that I can use find -exec to find and delete them. But I want a more practical solution.
To disable swap files from within vim, type
:set noswapfile
To disable swap files permanently, add the below to your ~/.vimrc file
set noswapfile
For more details see the Vim docs on swapfile
Set the following variables in .vimrc or /etc/vimrc to make vim put swap, backup and undo files in a special location instead of the working directory of the file being edited:
set backupdir=~/.vim/backup//
set directory=~/.vim/swap//
set undodir=~/.vim/undo//
Using double trailing slashes in the path tells vim to enable a feature where it avoids name collisions. For example, if you edit a file in one location and another file in another location and both files have the same name, you don't want a name collision to occur in ~/.vim/swap/. If you specify ~/.vim/swap// with two trailing slashes vim will create swap files using the whole path of the files being edited to avoid collisions (slashes in the file's path will be replaced by percent symbol %).
For example, if you edit /path/one/foobar.txt and /path/two/foobar.txt, then you will see two swap files in ~/.vim/swap/ that are named %path%one%foobar.txt and %path%two%foobar.txt, respectively.
I found the answer here:
vim -n <file>
opens file without swapfile.
In addition:
set dir=/tmp
in .vimrc creates the swapfiles in /tmp.
here are my personal ~/.vimrc backup settings
" backup to ~/.tmp
set backup
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set backupskip=/tmp/*,/private/tmp/*
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set writebackup
I agree with those who question why vim needs all this 'disaster recovery' stuff when no other text editors bother with it. I don't want vim creating ANY extra files in the edited file's directory when I'm editing it, thank you very much. To that end, I have this in my _vimrc to disable swap files, and move irritating 'backup' files to the Temp dir:
" Uncomment below to prevent 'tilde backup files' (eg. myfile.txt~) from being created
"set nobackup
" Uncomment below to cause 'tilde backup files' to be created in a different dir so as not to clutter up the current file's directory (probably a better idea than disabling them altogether)
set backupdir=C:\Windows\Temp
" Uncomment below to disable 'swap files' (eg. .myfile.txt.swp) from being created
set noswapfile
You can set backupdir and directory to null in order to completely disable your swap files, but it is generally recommended to simply put them in a centralized directory. Vim takes care of making sure that there aren't name collissions or anything like that; so, this is a completely safe alternative:
set backupdir=~/.vim/backup/
set directory=~/.vim/backup/
If you are using git, you can add *.swp to .gitignore.
If you put set directory="" in your exrc file, you will turn off the swap file. However, doing so will disable recovery.
More info here.
create no vim swap file just for a particular file
autocmd bufenter c:/aaa/Dropbox/TapNote/Todo.txt :set noswapfile
For anyone trying to set this for Rails projects, add
set directory=tmp,/tmp
into your
~/.vimrc
So the .swp files will be in their natural location - the tmp directory (per project).

Why does VIM create a X~ file for every X?

What is the purpose of the X~ files? And if its unnecessary, is there a way to prevent Vim from creating one?
This is a backup file that contains a copy of the previous revision of the file.
You can change this behavior by adding a line to the end of your .vimrc file:
set nobackup
its a backup file, every time you save while modifying something it will create one. There's an option to turn it off.
See here from the vim faq
What I personally do is that I create a folder $HOME/.vim/backupfiles that will contain all my backup files, instead of leaving them all scattered around.
My .vimrc contains the following:
set backupdir=~/.vim/backupfiles,~/tmp,.
This way you have the protection of backup files but avoid the littering of your folders with *~ files.
Turn it off using
:se nobackup
You can also tell it to put the backup file in a different location by using:
:se directory=c:\temp
Vim is creating backup files automatically. To turn it off you have to add the following line to your .vimrc config file:
set nobackup
They are backup files. Maybe :set nowritebackup will turn off this feature.

Resources