Vim E212: Can't Open File for Writing

Workaround available Linux, macOS Updated October 21, 2026

Quick answer

You don't have permission to write the file (usually a root-owned config). Save it through sudo with :w !sudo tee % > /dev/null.

Most likely fix: Save through sudo without losing edits.

Symptoms

After editing, :w fails with 'E212: Can't open file for writing'. Typical with files in /etc or after opening a file without sudo.

Why it happens

  • The file is owned by root or another user
  • The directory does not exist or is read-only
  • The filesystem is mounted read-only

How to fix it

Try these in order — the most common fix is first.

  1. 1. Save through sudo without losing edits

    1. Type :w !sudo tee % > /dev/null and press Enter
    2. Enter your password
    3. When Vim says the file changed, press L to load it, then :q
  2. 2. Add a w!! shortcut

    1. Add cmap w!! w !sudo tee % > /dev/null to ~/.vimrc
    2. Next time type :w!! to save as root
  3. 3. Save a copy and move it

    1. Run :w /tmp/file
    2. Quit, then sudo mv /tmp/file /path/to/file
  4. 4. Open privileged files the right way next time

    1. Use sudoedit /etc/file (or sudo -e) so your own Vim config is used and the save is done as root

This guide also covers

  • e212 can't open file for writing
  • vim save as root
  • vim sudo save
  • vim permission denied save

Still not working?

If Vim keeps failing, you're not alone. See every Vim problem users report or the alternatives people switch to.

Other Vim errors