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)
Related
So my question is if I am writing some code in vim & then want to go back 20 minutes earlier, then I type :earlier 20m in vim. But this doesn't work if I exit the vim once or even reboot by system. I understand that it is stored into the temporary registers and once vim restarts, it cleans the whole register buffers. But is there any way I can save the last changes & apply some undo mechanisms. Actually I work on some big project files & if something wrong occurs, I can not go back.
In Vim 7.3 or later, you can use an undo file.
Put this in your .vimrc.
set undofile
By default this will save 100 actions to undo. If you want more, you can set it manually by adding this to your .vimrc, with whatever number you want:
set undolevels=100
Persistent undo will create undo files in the same directories as the files you actually work on. If you want to put these in a separate directory so they don't clutter your filesystem, add this to your .vimrc:
set undodir=~/.vim/undo
Then, you need to actually make the directory. From the command line:
mkdir ~/.vim/undo
I would look into using version control instead of relying on vim's persistent undo to track changes in your projects.
I use CVS to sync my config files between different PCs, including my $HOME/.vim folder.
But there is a problem. CVS likes to leave old copies of files in .vim/plugin/CVS/Base/ and Vim thinks it should load these files. I want vim to ignore them!
You can use SourceCmd for this case:
augroup ForbidCVS
autocmd!
autocmd SourceCmd */CVS/* :" Do nothing
augroup END
At the bottom of $HOME/.vimrc add:
" Disable normal auto-loading of plugins
set noloadplugins
" Now load just the plugins we want
runtime! plugin/*.vim
This differs from the default behaviour which usually loads scripts from plugin/ and all its subfolders. I suspect most users do not make sub-folders. For users that do, this won't work!
An easy way to share your .vim and .vimrc across multiple computers is to put both on dropbox and symlink them to the $HOME directory of each computer you work on. No CVS required, and any change you make to your .vimrc for example, is immediately available to all other computers.
Another cool tip is to use the vim gsession plugin to create global session files stored to your .vim directory. So if you create a session on one computer, it is also available to all the others.
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
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).
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.