Why does the "*.txt~" file remain even after closing Vim? [duplicate] - vim

I've found that while using Vim on Windows Vim saves the file, a .ext.swp file that's deleted on closing the Vim window and a .ext~ file.
I assume the .ext.swp file is a session backup in case Vim crashes. What's the purpose of the .ext~ file however? Is this a permanent backup file? It's annoying as I'd like to copy all the files I'm working on to my host, without these duplicates. How can I turn this off or, if it's there for a good reason, hide the files?

I think the better solution is to place these lines in your vimrc file
set backupdir=~/vimtmp//,.
set directory=~/vimtmp//,.
The first line is for backup files, the second line for swap files. The double slash at the end ensures that there is no conflict in case of two files having the same name, see comments (at the time of this edit this option is only honored for swap files, not yet for backup files). The ,. allow vim to use the current directory if the former doesn't exist.
You have to create a directory in your home directory called vimtmp for this to work. Also, check that backups are enabled in your config (add set backup if not).
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.

The *.ext~ file is a backup file, containing the file as it was before you edited it.
The *.ext.swp file is the swap file, which serves as a lock file and contains the undo/redo history as well as any other internal info Vim needs. In case of a crash you can re-open your file and Vim will restore its previous state from the swap file (which I find helpful, so I don't switch it off).
To switch off automatic creation of backup files, use (in your vimrc):
set nobackup
set nowritebackup
Where nowritebackup changes the default "save" behavior of Vim, which is:
write buffer to new file
delete the original file
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.

To turn off those files, just add these lines to .vimrc (vim configuration file on unix based OS):
set nobackup #no backup files
set nowritebackup #only in case you don't want a backup file while editing
set noswapfile #no swap files

:set nobackup
will turn off backups. You can also set a backupdir if you still want those backup files but in a central folder. This way your working dir is not littered with ~ files.
You find more information on backups under :he backup.

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 (ORIGINALFILENAME-YYYYMMDD-HHMMSS.vimbackup).

Put this line into your vimrc:
set nobk nowb noswf noudf " nobackup nowritebackup noswapfile noundofile
In windows that would be the:
C:\Program Files (x86)\vim\_vimrc
file for system-wide vim configuration for all users.
Setting the last one noundofile is important in Windows to prevent the creation of *~ tilda files after editing.
I wish Vim had that line included by default. Nobody likes ugly directories.
Let the user choose if and how she wants to enable advanced backup/undo file features first.
This is the most annoying part of Vim.
The next step might be setting up:
set noeb vb t_vb= " errorbells visualbell
to disable beeping in vim as well :-)

You're correct that the .swp file is used by vim for locking and as a recovery file.
Try putting set nobackup in your vimrc if you don't want these files. See the Vim docs for various backup related options if you want the whole scoop, or want to have .bak files instead...

The only option that worked for me was to put this line in my ~/.vimrc file
set noundofile
The other options referring to backup files did not prevent the creation of the temp files ending in ~ (tilde)

I had to add set noundofile to ~_gvimrc
The "~" directory can be identified by changing the directory with the cd ~ command

Related

Vim won't save backup files into the correct directory

I have a vimrc file that has the following (along with a lot of other settings):
set backupdir=~/.vim-tmp
However, when I open a file foobar.txt using vim, change some text, save it and exit vim, a .footbar.txt~ file is left behind in the same directory as foobar. A few questions:
1) Shouldn't my setting in vimrc force this backup file to go into ~/.vim-tmp rather than the current directory?
2) Shouldn't the backup file be deleted when vim exits?
And just to verify, I searched for "backup" in my vimrc and only got the result at the top of the screen. Therefore, I don't think my setting is being overridden.
I figured my problem out on my own. What happened was my vimrc had set undofile however it did not have an undo directory; therefore, adding the following solved my issued:
set undodir=~/.vim/undodir

.vimrc backupdir several directories

I would like to create backup file in several directories after :w in vim, if statement is true. Vim :help says, that you need to put commas between directories and nothing else. But it's not working for me. It reads only the first directory. I tried different ways, such as usingset backupdir+=, or ~/. instead of ..
set backup
set nowritebackup
set backupdir=~/Dropbox
if expand("%")==".vimrc"
set backupdir=.,~/.vim/backUpDir/,~/Dropbox
endif
In .vimrc expand returns
:echo expand("%")==".vimrc"
1
vim --version
VIM - Vi IMproved 7.4
MacOS X (unix) version
I do not believe this is possible without custom plugin. If you read the vim help carefully, it says that the backup file will be created in the first directory in the list where this is possible. So the behavior you are seeing is by design.
*'backupdir'* *'bdir'*
'backupdir' 'bdir' string (default for Amiga: ".,t:",
for MS-DOS and Win32: ".,c:/tmp,c:/temp"
for Unix: ".,~/tmp,~/")
global
{not in Vi}
List of directories for the backup file, separated with commas.
- The backup file will be created in the first directory in the list
where this is possible. The directory must exist, Vim will not
create it for you.
If you really want to be able to backup to multiple directories, I would suggest writing a function to do this, and attaching it to BufWritePre.

How do I turn off automatic saving of a vim file with a ~ suffix

In my environment, I share vim configuration with other developers (and have my own configuration additions as well) in various .vimrc files. Some places in the environment, I edit a file in vim and automagically a copy of that file with a trailing tilde suffix appears. What vim options control this? I'd like to turn the feature off, as it just clutters up my directories and spoils auto-completion on the command line.
Thanks.
If you just want to turn off backups entirely, the :set nobackup suggestion from #GregHewgill is the way to go.
However, I've been saved by Vim's backups often enough that I'd like to recommend an alternative. I have these settings in my .vimrc:
set backupdir-=.
set backupdir^=~/tmp,/tmp
The first line removes the current directory from the backup directory list (to keep the ~ backups out of your working directories). The second tells Vim to attempt to save backups to ~/tmp, or to /tmp if that's not possible. (The paths can be adjusted for your particular environment; I use set backupdir^=$TEMP on Windows, for example.)
In other words, this puts the backup files out of sight (and out of your way), but doesn't turn them off entirely.
It sounds like you're looking for the nobackup option:
:set nobackup
Backups are not turned on by default, so you've probably got a config file somewhere that's turned them on.
On windows, I had set nobackup in C:\Users\my-user-name\_vimrc file; but that didn't seem to work. I then added set nobackup to C:\Program Files (x86)\Vim\_vimrc file. Still didn't work!
I later figured that C:\Program Files (x86)\Vim\_vimrc loads a sample vimrc script from $VIMRUNTIME/vimrc_example.vim and that script has this snippet:
if has("vms")
set nobackup " do not keep a backup file, use versions instead
else
set backup " keep a backup file (restore to previous version)
if has('persistent_undo')
set undofile " keep an undo file (undo changes after closing)
endif
endif
Commenting that out worked for me! So it really comes down to figuring out the order in which these scripts are getting executed, thereby overriding anything you might have done.
Two files will be created after editing. A file with "~" suffix, and another one with "un~".
Add the lines below in the _vimrc or .vimrc file:
set nobackup
set noundofile

GVIM on windows: way to disable the tmp file creation

I use gvim on windows and I want to know a way to disable the temp file(ending in ~) file creation. Also is there a problem if we do it?
You can disable the backup file by putting this line in your .vimrc:
set nobackup
I almost always do this, as the ~ file is more annoying that useful. There is no problem with doing this, you'll just lose the ability to revert to a backup of the file.
If you want to get rid of the temporary .swp (swap) file too, you can also set this:
set noswapfile
The swap file is created when you have a file open, and provides some backup/recovery security, in case Vim crashes while editing a file. It also can prevent multiple Vims from editing the same file. I usually just turn this off too, because I rarely have a use for it. The .swp file isn't as annoying as the ~ file, because it goes away when you close Vim, but I still just turn that feature off.
It's not quite what you asked for, but something that I've found works well is to redirect the swap and backup files to a seperate, dedicated folder. That way, they're still there if I need them, but they're not cluttering up the folder I'm working in.
The _vimrc file can be created in any of the following locations:
%HOMEPATH%\_vimrc
C:\Program Files (x86)\Vim\_vimrc
The following lines in the _vimrc file put backup files into a temporary directory:
set backup
set dir=%TMP%
set backupdir=%TMP%
set directory=%TMP%
set noundofile
The last line prevents the proliferation of undo files.
put these in your vimrc file
set nobackup
set nowritebackup
set noswapfile
From inside vim:
:e $HOME/_vimrc
and add this to the file:
set nobackup
Then, $HOME/_vimrc~ will hopefully be the last backup that vim makes!
You can also use the _vimrc from $VIM.
The _vimrc file from your home dir will be loaded later and the last setting wins.
Beware when using a common dir for backup, swap or undo files.
If you are on an USB or network drive and a drive letter gets reused you may run into problems.

Why does Vim save files with a ~ extension?

I've found that while using Vim on Windows Vim saves the file, a .ext.swp file that's deleted on closing the Vim window and a .ext~ file.
I assume the .ext.swp file is a session backup in case Vim crashes. What's the purpose of the .ext~ file however? Is this a permanent backup file? It's annoying as I'd like to copy all the files I'm working on to my host, without these duplicates. How can I turn this off or, if it's there for a good reason, hide the files?
I think the better solution is to place these lines in your vimrc file
set backupdir=~/vimtmp//,.
set directory=~/vimtmp//,.
The first line is for backup files, the second line for swap files. The double slash at the end ensures that there is no conflict in case of two files having the same name, see comments (at the time of this edit this option is only honored for swap files, not yet for backup files). The ,. allow vim to use the current directory if the former doesn't exist.
You have to create a directory in your home directory called vimtmp for this to work. Also, check that backups are enabled in your config (add set backup if not).
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.
The *.ext~ file is a backup file, containing the file as it was before you edited it.
The *.ext.swp file is the swap file, which serves as a lock file and contains the undo/redo history as well as any other internal info Vim needs. In case of a crash you can re-open your file and Vim will restore its previous state from the swap file (which I find helpful, so I don't switch it off).
To switch off automatic creation of backup files, use (in your vimrc):
set nobackup
set nowritebackup
Where nowritebackup changes the default "save" behavior of Vim, which is:
write buffer to new file
delete the original file
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.
To turn off those files, just add these lines to .vimrc (vim configuration file on unix based OS):
set nobackup #no backup files
set nowritebackup #only in case you don't want a backup file while editing
set noswapfile #no swap files
:set nobackup
will turn off backups. You can also set a backupdir if you still want those backup files but in a central folder. This way your working dir is not littered with ~ files.
You find more information on backups under :he backup.
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 (ORIGINALFILENAME-YYYYMMDD-HHMMSS.vimbackup).
Put this line into your vimrc:
set nobk nowb noswf noudf " nobackup nowritebackup noswapfile noundofile
In windows that would be the:
C:\Program Files (x86)\vim\_vimrc
file for system-wide vim configuration for all users.
Setting the last one noundofile is important in Windows to prevent the creation of *~ tilda files after editing.
I wish Vim had that line included by default. Nobody likes ugly directories.
Let the user choose if and how she wants to enable advanced backup/undo file features first.
This is the most annoying part of Vim.
The next step might be setting up:
set noeb vb t_vb= " errorbells visualbell
to disable beeping in vim as well :-)
You're correct that the .swp file is used by vim for locking and as a recovery file.
Try putting set nobackup in your vimrc if you don't want these files. See the Vim docs for various backup related options if you want the whole scoop, or want to have .bak files instead...
The only option that worked for me was to put this line in my ~/.vimrc file
set noundofile
The other options referring to backup files did not prevent the creation of the temp files ending in ~ (tilde)
I had to add set noundofile to ~_gvimrc
The "~" directory can be identified by changing the directory with the cd ~ command

Resources