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. Save through sudo without losing edits
- Type :w !sudo tee % > /dev/null and press Enter
- Enter your password
- When Vim says the file changed, press L to load it, then :q
2. Add a w!! shortcut
- Add cmap w!! w !sudo tee % > /dev/null to ~/.vimrc
- Next time type :w!! to save as root
3. Save a copy and move it
- Run :w /tmp/file
- Quit, then sudo mv /tmp/file /path/to/file
4. Open privileged files the right way next time
- 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
- How to Exit Vim (Stuck in Vim)
- Vim Copy to Clipboard Not Working
- Vim E325: ATTENTION Found a Swap File
- Vim E37: No Write Since Last Change
- Vim Backspace Not Working in Insert Mode
- Vim Paste Indentation Messed Up (Staircase Effect)
- Vim Colors Wrong in tmux
- Why Is Vim So Slow? (Large Files & Long Lines)
- Vim Arrow Keys Insert A B C D
- Vim Requires Python 3 Support Error (YouCompleteMe)
- Vim Modeline Vulnerability CVE-2026-34982