Monday, October 27, 2008

How to get rid of Vim's temp files

The default behavior in Vim when you save a file is to also put temporary files in the save directory. I'll explain how you can stop it from doing this. (I don't know Vim that well, but the following works).

What happens is that if you save a file called 'weeklyReport.txt' it also stores a temporary file with the same name appended with a tilde 'weeklyReport.txt~'. It also stores a swapfile there.

I found this really annoying, as it makes it awkward to scan the list of files in the directory (and the files were getting into my version control system - and yes, I'm sure I could find a way to avoid them getting in there, but I think the better solution is to not have them in the directory in the first place).

Here's a solution I found (I'm using it on Windows, but the only difference on other systems should be the file system syntax for specifying directories).

Open or create your settings file.

Here's some deails if you're not familiar with doing this.

When you install Vim, you've effectively installed two programs: Vim (the terminal version), and gVim (the GUI version), and each has its own settings file. For Vim, it's <VimDirectory>/_vimrc. For gVim it's <VimDirectory>/_gvimrc.

A note on what I mean by <VimDirectory>: the actual exectuable files are stored in a directory containing the vim version number - on my computer it is C:\Program Files\Vim\vim72. This is not the <VimDirectory> I'm referring to - the one I'm referring to is the parent directory of that, in my case C:\Program Files\Vim
Add the following lines to it (it shouldn't matter where):
" turn on backup
set backup

" Set where to store backups
set backupdir=c:\temp

" Set where to store swap files
set dir=c:\temp
This will store the tilde and swap files in c:\temp - you can change the directory location to wherever you want.



I found this soultion here, via this, and that via a google search.

8 comments:

  1. What crappy version control system are you using?

    tilde files have mean `backup' on systems for decades now, everything knows to ignore them.

    I would advise you to at least put the backups in somewhere more useful than temp; if you use vi outside your version control system one day and just happen to need that backup file, you'll want it outside of temp.

    ReplyDelete
  2. Hi Clinton, thanks for the feedback.

    I'm using Subversion. It's not for code, just for the text files containing my phd writing, and i'm using it in a pretty basic way.

    i just use

    svn add *
    svn commit -m ""

    to get all the changes in there.

    you'd think that I could tell it to just use

    svn add *.txt
    and
    svn commit *.txt -m ""

    but IIRC that also adds the tilde files for some reason.

    Regarding the temp directory, I guess I'd seen that as safe as any other directory... it's contents never seem to get deleted by anything other than me.. or would there be something I'm missing?

    BTW, I got a copy of that book Structures recently and started reading it... seems quite good.

    ReplyDelete
  3. just add:

    set nobackup

    to your vimrc files...

    ReplyDelete
  4. @Lawrence, that does not always work. The original post is correct.

    ReplyDelete
  5. you must also do 'set nowritebackup' or it will still write backup files.

    ReplyDelete
  6. Those backup files are a PITA. I'm tired of having to delete them. With version control they are totally useless. Thanks for the help everyone.

    ReplyDelete
  7. Hey James, even 4 and a half years after you posted this, you have helped me resolve this issue.

    Just wanted to note, for any future readers of this post, if you are hesitant to use the temp folder, what I did was I simply created a folder in my main programming directory called "backup" and set all of my backups to be saved to there. That way they are all there, easily accessible if I ever need them.

    This saves me much annoyance, as when I go to compile/open/etc a file in a terminal and use "tab" to quickly fill in the rest of the name of the file (instead of explicitly typing it out), I don't get a "Did you mean .c or .~c" message anymore.


    Anyways, just wanted to say thanks for posting this solution!

    ReplyDelete
  8. Hi Anthony, glad that it was a help.
    James.

    ReplyDelete