Tuesday, September 13, 2011

clear vim backup file ~ extension filename

# find /www/drupal_center/sites/all/modules/custom -name '*~' -type f -print0 | xargs -0 -I @ rm @

Thanks for the information, it helped out a lot. I would like to add a few comments about the above information.

set nobackup #no backup files

Having this set will not leave any additional file(s) around after having closed VIM. This is what most people might be looking to have set.

set nowritebackup #only in case you don't want a backup file while editing

I do not set this one. The default is :set writebackup This will keep a backup file while the file is being worked. Once VIM is closed; the backup will vanish.

Where nowritebackup changes the default "save" behavior of Vim, which is:
1. write buffer to new file
2. delete the original file
3. rename the new file

and makes Vim write the buffer to the original file (resulting in the risk of destroying it in case of an I/O error). But you prevent "jumping files" on the Windows desktop with it, which is the primary reason for me to have nowritebackup in place.

set noswapfile #no swap files

From the VIM help file.
- Don’t use this for big files.
- Recovery will be impossible!
In essence; if security is a concern, use noswapfile. Keep in mind that this option will keep everything in memory

I think the better solution is to place these lines in your vimrc file:

" controls where backup files (with ~ extension by default) go.
set backupdir=~/.vimbak

" The 'directory' option controls where swap files go.
set directory=~/.vimbak

You have to create a directory in your home directory called vimtmp for this to work.

That way you get the benefit of both worlds, you don't have to see the files, but if something does get futzed you can go get your backup file from vimtmp. Don't forget to clean the directory out every now and then.

And you can also set a different backup extension and where to save those backup (I prefer ~/.vimbackups on linux). I used to use "versioned" backups, via:

au BufWritePre * let &bex = '-' . strftime("%Y%m%d-%H%M%S") . '.vimbackup'
This sets a dynamic backup extension (ORIGINALFILENAM-YYYYMMDD-HHMMSS.vimbackup).

you might want to include how to set up the back up directory, i.e. putting set backupdir=~/.vimbackups in your ~/.vimrc

The vim help fully describes this technique. See :help backupext for file extension which is appended to a file name to make the name of the backup file.



No comments: