Unable to write changes to a file in Vim editor

12,718

The message file is read only - press ! to override means that you don't have permissions to write to the file which you are editing, so the changes you have made can't be written in that file.

Easiest solution is to write the edited file content to the other file with :w file_name, assuming that file_name is a path to a file in which you do have permissions to write.

Other than that you need to find out why you don't have permissions to write to your original file.
You can do that with ls -l file_name.
(I have explained the output of ls -l file_name here.)

Now that you understad a bit more about permissions, you have 3 options:

  1. Use sudo vim file_name to edit the file as the root user.

  2. Give other users write permissions with chmod.

  3. Change the owner of the file with chown.

Share:
12,718

Related videos on Youtube

Aaron Brandhagen
Author by

Aaron Brandhagen

I used to think I was fairly intelligent. Then I started learning to code. It's been about a year and a half and I feel progress finally. I have been a Tech Recruiter for my entire professional career. Shortly after, Lynda was calling my name. I hated pretending that I knew anything about the positions I was recruiting on. Plus, I'm a geek at heart. Hardcore. Played sports all my life, big outdoors-man - but I've never been more dedicated to anything in my life that I have been to learning computer science. So! Even though I am quite certain I'm not half as smart as other in my shoes and in the industry, but I recently had my own confirmation that I can do this. And I'm pumped! My dream to to join Ashton Kutcher's team at Thorn, a cyber security firm that fights the war against sexual abuse and trafficking of children over the dark web. Glad to meet ya!

Updated on September 18, 2022

Comments

  • Aaron Brandhagen
    Aaron Brandhagen over 1 year

    I have a 100 GB of xml doc that I'm migrating to a database in waves.
    In vim, I can edit the file, but I'm unable to save changes with :wq, :q or :xx.

    I get the error message this file is read only - press ! to override.
    Nothing works, so I use the :q! which ignores my changes and exits the vim.

    How can i save my changes?

    Bonus question

    If I don't wait for the entire file to load into memory and press Ctrl + c to view what has been populated, will saving that document only save what was loaded into the memory and delete the rest?

  • Aaron Brandhagen
    Aaron Brandhagen almost 6 years
    Ahhhh, so that's why you always use sudo to open files. I thought that was just to access them if permission was needed. Perfect. Thank you, sir!
  • Iskustvo
    Iskustvo almost 6 years
    @AaronBrandhagen Please don't get the wrong idea, you don't always use sudo, just when you have to. I have updated the answer to give you more options if those make sense in your case.
  • Aaron Brandhagen
    Aaron Brandhagen almost 6 years
    Even better. Seriously this is a great help, appreciate it.
  • Iskustvo
    Iskustvo almost 6 years
    You can show your appreciation for questions, answers and even comments by voting them up so that they can appear at the top and to award the person who posted it. It is also good te upvote other questions to make them easier to find in searches. You can read that in unix.stackexchange.com/tour Let me just clarify that you are neither obligated nor expected to do that now, I just see that this is your first question and wanted to inform you. As for the bonus part, I don't have a large file to try this, but I think you can try it yourself now that you can save files properly.
  • Aaron Brandhagen
    Aaron Brandhagen almost 6 years
    Thanks for the heads up. I did try to upvote, but I don't have the rep yet so it won't let me. I definitely would if I could! And I appreciate your disposition in telling me this - huge difference than what I'm used to with SO. I make sure to back track once I hit the 15 mark and give credit. You absolutely deserve it for the help
  • Iskustvo
    Iskustvo almost 6 years
    I totaly forgot about that limit, sorry for bothering you than :D I just see a lot of new people forgeting to accept the answer or upvote it, so I thought to mention it. Don't worry about this one, it's not that important :D Best of luck in Unix & Linux!
  • Aaron Brandhagen
    Aaron Brandhagen almost 6 years
    Don't even mention it! No worries. I more than appreciate good help that without would otherwise leave me struggling for hours. Giving you credit is the bare minimum I can do - I'll absolutely give you the credit! I'd want the same no doubt.