2 minute read

Context

You open a file in Vim, make your edits, and then hit save—only to see:

E212: Can't open file for writing

You forgot sudo.

This happens constantly when editing system files, configs, or service definitions. Fortunately, Vim provides a safe escape hatch that lets you write the file without losing your changes or restarting the editor.


The Classic Vim Trick

From Normal mode, run:

:w !sudo tee %

Then press Enter and provide your sudo password.

This writes the file with elevated privileges while keeping your Vim session intact.


What This Command Actually Does

Breaking it down:

  • :w → write the current buffer
  • ! → pipe output to an external command
  • sudo tee → write input to a file with root permissions
  • % → current file name

Vim sends the buffer contents to tee, which writes the file as root.


Why This Works (and :w Didn’t)

When you opened the file:

  • Vim was running as your user
  • the file required elevated permissions to write

Instead of restarting Vim with sudo, you elevate only the write operation, not the entire editor session.

This reduces risk and keeps context intact.


Suppressing the Extra Output

By default, tee echoes the file contents back to the terminal.

To suppress that, use:

:w !sudo tee % > /dev/null

This keeps your screen clean while still writing the file.


Confirm the File Was Written

After saving, reload the buffer to confirm ownership and permissions:

:e!

This ensures Vim is displaying the file as it exists on disk.


When This Is Appropriate

This technique is ideal for:

  • quick config edits
  • one-off fixes
  • emergency changes
  • systems where reopening the editor is disruptive

It’s a productivity shortcut—not a permission model.


When You Should Avoid It

Avoid this approach when:

  • performing large or risky edits
  • making repeated changes to protected files
  • working in audited or regulated environments
  • you actually need a root shell

In those cases, opening the editor with sudo vim may be more appropriate.


A Safer Default Habit

To avoid this situation entirely:

  • open protected files explicitly with sudo
  • or use tools like sudoedit:
sudoedit /etc/myconfig.conf

This edits the file as your user and writes it back safely.


Common Mistakes

  • Forgetting to reload the buffer after writing
  • Using this trick blindly without understanding it
  • Running Vim itself as root unnecessarily
  • Copying the command without knowing what % means

Understanding the mechanics prevents accidents.


Practical Tips

  • Keep this command in muscle memory
  • Use the /dev/null variant to avoid clutter
  • Prefer sudoedit for longer sessions
  • Reload the file after writing
  • Treat elevated writes with care

Small tricks like this add up over time.


Takeaways

  • You don’t need to restart Vim with sudo
  • :w !sudo tee % safely elevates only the write
  • This preserves context and avoids lost work
  • It’s ideal for quick fixes
  • Understanding permissions keeps you productive

Forgetting sudo is annoying—but Vim gives you a graceful recovery.