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.confor/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 444orchmod 644owned by another user) prohibiting write access. - Opening Files in View Mode: Starting Vim using the
viewcommand 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.
- Ensure you are in command mode by pressing Esc.
- Type the following command sequence and press Enter:
:w !sudo tee % - Enter your user account password when prompted by
sudo. - Vim executes
teewith elevated administrative privileges, writing the current buffer contents into the file referenced by%(the current file path). - When Vim displays a message prompting ‘File has been modified since reading… Press Enter or type command to continue’, press Enter.
- Reload the updated buffer to synchronize Vim’s internal state:
:e! - 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.
- Press Esc to enter normal mode.
- Type:
:w! - 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.
- Press Esc to enter normal mode.
- Disable the buffer’s read-only flag by executing:
:set noreadonly - If the file is also marked as unmodifiable, re-enable editing by running:
:set modifiable - 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.
- Press Esc to enter command mode.
- Write the buffer to a temporary file in your home directory:
:w ~/temp_config.txt - Exit Vim with
:q!. - 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.
- Exit Vim by typing
:q!. - Check the mount flags of your filesystems:
mount | grep "ro," - If the filesystem is mounted in
romode due to an error, remount it as read-write (or schedule anfsckcheck on next reboot):sudo mount -o remount,rw /