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 ?


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.


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

    set backupdir=~/vimtmp,.
    set directory=~/vimtmp,.
    

    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.


    要关闭这些文件,只需将这些行添加到.vimrc(基于Unix的OS上的vim配置文件):

    set nobackup       #no backup files
    set nowritebackup  #only in case you don't want a backup file while editing
    set noswapfile     #no swap files
    
    链接地址: http://www.djcxy.com/p/7174.html

    上一篇: Git中“我们”和“他们的”的确切含义是什么?

    下一篇: 为什么Vim用〜扩展名保存文件?