Stop Vim From Crapping In My Directories!

Monday, 03 March 2014
  • By
  • Jeff Ammons
  • Tags:
  • Vim
  • Tools

Vim poo on carpet

Not on the carpet, Vim!

If you use Vim you've no doubt seen the steaming piles of poo it leaves in your working directories.

For example if you open foo.txt in Vim, it creates foo.txt~ in the same directory. I probably see the tilde as a pile of poo because I own a dog. Now that I've said it, you will see it as poo too. You're welcome.

In addition to the ~ file, it will also create a .foo.txt.swp file.

In some cases this is no problem. If you are working on code it may or may not be a problem.

What Are These Poo Files?

The tilde files are backup files. When you open a file in Vim, it first copies the file to the same name + tilde. This can be a lifesaver if something crashes.

The swp file is a working file. Vim keeps notes on what it's doing. Undo, redo, etc.

The files are ugly, but useful. They are just inconveniently located.

So how do we fix this?

Fix #1 .gitignore

If you are using Git, you can of course put the swp and ~ files in your .gitignore file so they don't get added to the repository.

They will still be there in your working directory, but not in your repo.

Here's a sample for your .gitignore that will protect the repo from Vim poo.

### vim ###

Note: For this and all your .gitignore needs, visit Spatula City

You should do this even if you do any of the other fixes. That way you're covered if in the future you rebuild your machine and miss something when configuring Vim.

If you aren't using Git, most source control systems have something similar.

Fix #2 Turn The File Creation Off

Right off the bat I have to say I don't like this fix.

Those files serve a very valid purpose.

BUT, if you don't want them at all, Vim lets you disable them.

I'm assuming you're familiar with your .vimrc file. A deep dive on that is beyond the scope of this post, so you'll have to look it up. Start with Vim Doc.

If you're new to Vim, trust me, you'll want to read up on your .vimrc file…

set nobackup
set nowritebackup
set noswapfile

Now you'll have no extra files created. You'll also be a sad panda at some point in the future…

Fix #3 Save Them Somewhere Else

I prefer this approach over turning the files off.

You can go simple or complex here. If you go simple, you have to make sure the temp directory exists. If you go complex, then you have to get a fairly complex bit of gibba jabba in your .vimrc file.

Go Simple

Also in your .vimrc file, you can put the following:

set backupdir=~/vimtemp,.
set dir=~/vimtemp//,.

In this case you need to have a vimtemp directory in your home directory, otherwise it will fall back to the current directory thanks to the ",.".

Note: If you aren't familiar with the dot used here, it just means “the current directory.” In other words you revert to the default behavior of putting the temp files in the same directory as the file you are editing.

Backupdir is the directory where you want the backup files (the ones with poo on them tildes following them).

Dir is the directory for the swap files.

Here they are the same directory, but they don't have to be.

If you notice the dir ends in two slashes. When you do that the swap file will include the full directory path of the file you are editing. That should prevent collisions if you have files with the same name in different directories (like .gitignore).

Unfortunately this doesn't seem to work for the backup files.

This is the route I prefer. I think for most people this will work just fine.

For more info you can try these commands in Vim:

	:help dir
    :help backupfile

Go Complex

As I said, I like simple. I don't mind creating a vimtemp directory myself.

The complex route is just additional code in your .vimrc file to create the directory if it doesn't exist.

A good starting point for this is Automatically create tmp or backup directories in the Vim Wiki.

#More Vim Posts Here's another post I wrote on Vim that you might find useful.