Since the beginning of Vim, a particular feature of Vim has continually clashed with version control software. When you save a file, Vim compares the timestamp of when it last read it to its current modification time. If they are different, Vim prompts the user:

WARNING: The file has been changed since reading it!!!
Do you really want to write to it (y/n)?

You've changed branches or performed some other Git operation that updated the file you have open. And that means a different modification time.

Why it sucks

This warning requires extra keystrokes for each buffer you're working with. It's jarring in CLI Vim because it is 2+ lines of text, shoving up your entire UI until you answer. Worse, sometimes Vim doesn't refresh properly afterwards, causing your UI to be off and requiring you to relaunch or open/close a tab.

It can also happen when only temporarily switching to another branch. Say you wanted to check something on master real fast, and didn't want to use a Git worktree. You switch, and switch back again. Despite your files being exactly the same, Vim is tipped off that they changed. So then you get to fight through this warning for each file you have open.

The solution

To disable this warning, you'll download NeoVim with a custom feature, build it, and install it in isolation from your stock Neovim instance, and turn on the feature.

FAIR WARNING: This idea lets your VCS (Git) handle scenarios where file contents may have changed. You should only do this if you feel that the warning gets in your way more than it saves you from yourself. It's now your responsiblity to manage when you are overwriting a change, and if you are unprepared this may get you in trouble. You should always review the changes you're committing beforehand with a git diff. If you don't, removing this warning will definitely cause you to overwrite valid changes. The method described in this post requires you to know when you need to refresh your Vim buffer manually (:e).

If you are not comfortable with that, don't do this. The feature was written for a reason.

  1. Check out my Neovim fork with the overwritewarning feature.

    git checkout https://github.com/mcnelson/neovim.git
    
  2. Install Libtool via your package manager. I.e. on Fedora: dnf install libtool.

  3. Follow the instructions on the Neovim Wiki Page to install this custom fork of NeoVim. This method installs Neovim stand-alone at ~/neovim, so it won't interfere with your system Neovim instance. That way, if the feature is ever merged to the main project, you can simply remove ~/neovim and update your main version.

    rm -r build
    make CMAKE_EXTRA_FLAGS="-DCMAKE_INSTALL_PREFIX=$HOME/neovim"
    make install
    
  4. Add the binary to your PATH in your Bash profile -- .bashrc or .profile. This serves as the switch between custom and stock Neovim.

    export PATH="$HOME/neovim/bin:$PATH"
    
  5. Launch nvim and open your ~/.config/nvim/init.vim. Add:

    set noow # No overwrite warning
    
  6. Source your config file to establish the new setting.

    source %
    
  7. Test that it's working:

    nvim /tmp/foo
    

    With Neovim open, separately run:

    touch /tmp/foo
    

    Back to Neovim, and run :w. It should now quietly overwrite the file.

I would love to see this feature merged and released into the main Neovim repo. I will update this post when/if that happens!

More blog posts