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).
Related
I have recently tried to aid in automating my workflow in vim. Since there wasn't a .vimrc file in my home folder by default, I've made a new one and put my code in. After starting up vim again I was surprised to see my C code unhighlighted. After removing .vimrc file, everything was back to normal. Knowing this, I would assume vim falls back to some internally defined .vimrc file when there isn't one in the user folder. Is there a way to load a user-made .vimrc file without overriding system default settings? If not, is there a list of commands that I can put into my .vimrc file to restore those settings?
If not, is there a list of commands that I can put into my .vimrc file to restore those settings?
Yes, simply add the line runtime defaults.vim on top of your vimrc.
I have created ctags for my project. Now every time, I open file in vim editor I set ctag location. Is it possible to set the ctag location for one time in some where that I don't know, So that when I open a file from my project in vim, it will start working, I do not need to fire this command set tags=~/tags.
Please help.
Use the :echo $MYVIMRC within Vim to print the location of your .vimrc file. If the variable is not set, you'll want to create the file yourself. Typically its location is ~/.vimrc, but as the comments mention there are other supported locations. Once you've created the file, you can just add the command as you normally would: set tags=~/tags. As other comments mentioned, it's probably for the best to put your tags file in some constant, relative-to-the-project-root-folder place. I personally follow Tim Pope's ctags guide for all my Git projects.
I mean, which would you rather have,
set tags=./tags
or
set tags=/some/project/tags,/some/other/project/tags,/yet/another/project/tags,...
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.
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)
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.