Vim is an incredibly powerful and customisable text editor, but like any complex tool, it can sometimes present challenges, especially when using various plugins and features. Recently, I encountered a couple of frustrating issues that interrupted my workflow: Vim repeatedly prompted me to force-save a file due to read-only errors, and my screen would freeze when using the fzf.vim plugin to navigate search results. Here’s how I diagnosed and fixed these problems.

Problem 1: Read-Only Errors When Saving Files

While working on my Python code, I noticed that every time I tried to save my changes, Vim responded with the following error:

E45: 'readonly' option is set (add ! to override)

This error indicates that Vim is treating the file as read-only, even though I was the file’s owner and had full write permissions according to the file’s permissions:

-rw-r--r--    1 myself  staff   46560 12 Aug 14:54 my_python_code.py

Diagnosing the Issue

Upon further investigation, I found several Vim swap files in the directory where my script was located:

-rw-r--r--    1 myself  staff    4096 12 Aug 15:01 .my_python_code.py.swn
-rw-r--r--    1 myself  staff    4096 12 Aug 14:57 .my_python_code.py.swo
-rw-r--r--    1 myself  staff  131072  7 Aug 15:55 .my_python_code.py.swp

These files are automatically created by Vim to allow recovery of unsaved changes in case of a crash. The presence of multiple swap files (.swo, .swn, and .swp) indicated that Vim might have opened the file multiple times in different sessions or that a previous session didn’t close properly.

The Solution

To resolve the issue, I simply removed these swap files:

rm .my_python_code.py.swp .my_python_code.swo .my_python_code.py.swn

After deleting the swap files, I reopened my_python_code.py in Vim, and the read-only error was gone. I could save my changes without needing to force the write.

Takeaway

If you encounter a similar issue, where Vim insists that a file is read-only despite proper permissions, check for lingering swap files. Removing these files can often resolve the issue, allowing you to continue editing without interruption.

Problem 2: Freezing Screen with fzf.vim

Another issue I encountered was with the fzf.vim plugin, which integrates the fuzzy finder fzf with Vim for powerful file searching and navigation. After searching for a term using Rg (a command that leverages ripgrep to search within files), I navigated to a file that contained the search term. However, as soon as I hit Enter to open the file, the screen froze.

Diagnosing and Solving the Issue

Interestingly, after removing the Vim swap files that resolved the read-only error, the freezing issue with fzf.vim also disappeared. It turns out that the presence of multiple swap files not only caused the read-only error but also interfered with how Vim and fzf.vim managed file navigation and buffers, leading to the freezing problem.

Takeaway

If you experience freezing issues with fzf.vim, especially when navigating search results, consider checking for and deleting any swap files associated with the files you’re editing. These swap files can cause unexpected behaviour beyond simple read-only errors, including freezing and navigation issues.

Conclusion

Vim’s power lies in its flexibility and extensibility, but with great power comes the occasional hiccup. Whether it’s dealing with read-only file errors or freezing issues with plugins, a methodical approach to troubleshooting can help you quickly resolve these problems and get back to work.

Happy Vimming!