Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

ViM: Unable to open swap file

📅 2010-Jan-14 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

 

Problem

E303: Unable to open swap file for "[No Name]", recovery impossible

You might see this error while using ViM as a standard user on Windows 7. This is a result of ViM still catching up to being used as a standard user on Windows 7.

By default, ViM opens a swap file when the user starts editing an already existing file. On Windows, the swap file is a hidden dot file named the same as the opened file with a suffix of .swp. For example, editing hello.cpp will create a hidden .hello.cpp.swp. This swap file is created in the same directory as the opened file.

If the user just opened ViM and started editing into the empty editor, what happens? On Windows, a hidden swap file named _.swp is created. The above error is caused because ViM on Windows 7 tries to create this swap file in C:\Windows\System32. A standard user does not have the permissions to create files there and thus ViM throws the E303 error.

Solution

The solution would be to tell ViM to create the swap files in a temporary directory where a standard user has all the necessary permissions. A good location would be the directory pointed to by the %TEMP% environment variable. On Windows 7, this points to C:\Users\AverageJoe\AppData\Local\Temp.

The directory where ViM creates its swap files is read from the dir (or directory) variable. Setting it to %TEMP% solves the above error:

:set dir=$TEMP

Note that ViM swap file behaviour changes a bit when the dir variable is set. All swap files are created in this directory, as opposed to being created in the directories of the files being edited. And the swap filenames are not prepended with a dot. For example, editing D:\Work\hello.cpp creates a hidden swap file C:\Users\AverageJoe\AppData\Local\Temp\hello.cpp.swp