How to Fix ‘Vim: E45: readonly option is set (add ! to override)’ in Linux

Editing configuration files or scripts in Linux using Vim only to see the error message on the bottom status bar: E45: 'readonly' option is set (add ! to override) stops you from writing changes to disk with standard :w commands. This alert indicates that Vim detected that the opened file or current buffer has been marked with the read-only attribute. While Vim reminds you that appending an exclamation mark (:w!) can override soft read-only flags, this override fails with E212: Can't open file for writing if your shell user account lacks filesystem write permissions.

Why Does Vim Report E45: readonly option is set?

Vim sets the read-only flag on buffers for several practical reasons:

  • Opening Root Configuration Files Without Sudo: Launching files inside /etc/ (such as /etc/nginx/nginx.conf or /etc/fstab) as a regular user, causing Vim to protect you from writing to unprivileged files.
  • Linux Filesystem Permission Restrictions: The file mode bits (e.g. chmod 444 or chmod 644 owned by another user) prohibiting write access.
  • Opening Files in View Mode: Starting Vim using the view command or passing the -R (readonly) flag on the command line.
  • Read-Only File System Mounts: The storage partition being mounted in read-only mode due to disk errors or container storage configurations.

Step 1: Save Root Files Without Quitting Vim Using Sudo Tee

If you spent time modifying a system file only to realize you forgot to prefix the command with sudo, do not discard your edits. Use the external tee pipeline trick directly within Vim.

  1. Ensure you are in command mode by pressing Esc.
  2. Type the following command sequence and press Enter:
    :w !sudo tee %
  3. Enter your user account password when prompted by sudo.
  4. Vim executes tee with elevated administrative privileges, writing the current buffer contents into the file referenced by % (the current file path).
  5. When Vim displays a message prompting ‘File has been modified since reading… Press Enter or type command to continue’, press Enter.
  6. Reload the updated buffer to synchronize Vim’s internal state:
    :e!
  7. You can now safely exit Vim by typing :q.

Step 2: Use the Force Override Command (:w!)

If you own the file and possess write permissions, but Vim loaded the file with a soft read-only flag (e.g. opened with vim -R), simply force the save.

  1. Press Esc to enter normal mode.
  2. Type:
    :w!
  3. Press Enter. Vim overrides the read-only buffer attribute and commits the changes to disk.

Step 3: Toggle the Readonly Buffer Flag in Vim

You can dynamically disable the read-only status of the current Vim buffer.

  1. Press Esc to enter normal mode.
  2. Disable the buffer’s read-only flag by executing:
    :set noreadonly
  3. If the file is also marked as unmodifiable, re-enable editing by running:
    :set modifiable
  4. Attempt saving the document normally using :w.

Step 4: Save to an Alternate Location and Fix Permissions

If directory-level permissions prevent write access entirely, save your modifications to your local home directory first.

  1. Press Esc to enter command mode.
  2. Write the buffer to a temporary file in your home directory:
    :w ~/temp_config.txt
  3. Exit Vim with :q!.
  4. Use elevated permissions to adjust the destination file permissions or overwrite it:
    sudo cp ~/temp_config.txt /path/to/original/file
    rm ~/temp_config.txt

Step 5: Check Filesystem Mount Status

If every file on the system reports read-only errors, your partition may have been remounted read-only following filesystem errors.

  1. Exit Vim by typing :q!.
  2. Check the mount flags of your filesystems:
    mount | grep "ro,"
  3. If the filesystem is mounted in ro mode due to an error, remount it as read-write (or schedule an fsck check on next reboot):
    sudo mount -o remount,rw /

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.