vim unable to write to files even though sudo is used

6,633

This is the intended behavior of vim (and vi).

When you edit a readonly file, attempting to write the file the usual way (with :w) fails. This is to prevent you from accidentally changing a readonly file you might not wish to change.

If you really want to override the readonly permissions on a file that you own, and write your changes to the file, you must use the :w! command so vim knows this is what you want.

  • This is similar to how :q will not quit if there are unsaved changes, but :q! will.

If you want to write any changes and quit in one command, you can use :wq! or :x!.

Share:
6,633

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I find that with vim, if I try and edit a file which as a normal user I do not have write access with, even if I use sudo, I am unable to write to it, although if I use nano to edit the same file it works.

    So for instance if I do:

    sudo vim /var/path/to/file.conf
    

    I will get this in the file and not be able to edit that file:

    "/var/path/to/file.conf" [readonly]
    

    But if I instead do:

    sudo nano /var/path/to/file.conf
    

    It will be able to write to the file, why is this, why does sudo not give vim write access like it does with nano? Is this some sort of bug? Or is this just something which is meant to be? Because it is very annoying.


    OS Information:

    Description:    Ubuntu 15.04
    Release:    15.04
    

    Package Information:

    vim:
      Installed: 2:7.4.488-3ubuntu2
      Candidate: 2:7.4.488-3ubuntu2
      Version table:
     *** 2:7.4.488-3ubuntu2 0
            500 http://gb.archive.ubuntu.com/ubuntu/ vivid/main amd64 Packages
            100 /var/lib/dpkg/status
    
    • NuclearPeon
      NuclearPeon almost 9 years
      It could be hosted on a read-only filesystem. run mount to see if /var/path/to is located in the output, then post what filesystem type it's on.
    • Eliah Kagan
      Eliah Kagan almost 9 years
      @NuclearPeon nano can write to the file, so it's not on a readonly filesystem.
    • NuclearPeon
      NuclearPeon almost 9 years
      Although there is already an accepted answer, maybe this will help: askubuntu.com/questions/471776/wq-on-vim-does-not-save - could be a difference between vi and vim
    • Eliah Kagan
      Eliah Kagan almost 9 years
      @NuclearPeon In that question, the user cannot override lack of write permissions because they do not own the file. The file is owned by root and set writeable only by root; thus one must sudo to root to edit it (or use sudoedit, which does the same thing behind the scenes on exit). Since the file has write permissions set for root, ! isn't needed to save changes when editing it as root. In this question, the user owns the file, but it's (probably) not set writable for anyone, so there's no reason to edit as root (and ! is needed even with sudo).
  • MrSaooty
    MrSaooty almost 9 years
    This will also happen if you've accessed the file with -R. you could read about this and more with :help readonly from within vim, from the manpages.
  • Arnold Roa
    Arnold Roa over 7 years
    It wont work when using sudo, even with :w! any idea?
  • Scotty Jamison
    Scotty Jamison over 2 years
    If it doesn't work, even with w!, perhaps the file has an immutable attribute set. See here for more info and details on how to remove it: unix.stackexchange.com/a/67509/365977