47

Need help. I have been editing a text file in vi , and i was able to save the changes before but now i am getting the above error whenever i am typing command to save the file. Please help .

choroba
  • 231,213
  • 25
  • 204
  • 289
Aditya
  • 549
  • 1
  • 6
  • 11

13 Answers13

58

You can, as vi (or, more likely, vim) is saying force a write by doing:

:w!

Alternatively, write the file to an alternative location:

:w! /path/to/other/location

To check where your backup files are being written (normally):

:se backup? backupdir? backupext? 

Try to touch a file in that directory and see if your Operating System gives you an error to enlighten you as to why your editor cannot write there.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 24
    In my case the backups folder (`~/.vim/backups`) didn't exist. I created it, and the error went away. – Sheharyar Jan 10 '15 at 20:29
  • 4
    +1 for pointing out `:se backup? backupdir? backupext?` ! ---- In my case the subdirectory `backup` (as specified by the `backupdir` option value) was missing along with my plugins from a corrupted `~/.vim/` directory. – Cbhihe Mar 20 '16 at 13:30
  • 1
    didnt know how it end up folders like ~/.vimundo, ~/.vimbackup are owned by root. Changing owner back to me solves the problems. `sudo chown -R peng:peng ~/.vim*` – Peng Zhang Aug 02 '16 at 03:40
  • 2
    In my case the backupdir was ~/tmp and didn't exist. Creating it solved the issue. – mike23 Jun 27 '19 at 06:49
  • This error occurs after you set the backup folder to peculiar one with `set backupdir=$PATH`, and `$PATH` does not exist. – Yang Liu May 27 '21 at 15:29
  • @Sheharyar : in my case I added ~/.vim/backup (singular), backups (plural) did not work. – Herman Jaramillo May 11 '22 at 19:45
  • i had to add ~/.backup and it went away, but i had /tmp// defined last, not sure why the double slash but i think i remember thats how it would work, but there is no explanation to this why it cant use /tmp anymore, i can touch a file there, and the last tmp one was my user owned, and 644 already anyway. – blamb Aug 03 '23 at 22:27
8

Had the same problem. It was because I installed vimconf as root. You need to change rights of files in ~/.vim directory and change owner to your user.

sudo chmod 0750 ~/.vim
sudo chown user ~/.vim
Piotr Pęczek
  • 410
  • 7
  • 14
8

Had the same problem. Tried all options as above but it did not work. Then when I checked my disk space, it was full. Once I cleared some space then I was able to write back to file again.
P.S: This was in linux.

naamadheya
  • 1,902
  • 5
  • 21
  • 28
4

I had this same problem. Turns out it was due to running out of disk space. try creating a file using Ex) touch test.txt. If you get a message saying touch: cannot touch test.txt: No space left on device you will need to clear up space on your disk

Aaron Oommen
  • 770
  • 6
  • 9
3

I don't know what the cause was, but I moved by backupdir from . to ~/.vim/backups in my .vimrc and it solved it for me:

set backupdir=~/.vim/backups

I'd imagine some sort of tool was using the folder the file I was editing it in (Visual Studio 2013, in my case), but I'm not sure.

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
3

I've fixed this with:

sudo chown {user} ~/.cache/vim/* -R 

where the "{user}" field is your user-name.

Manuel Schmitzberger
  • 5,162
  • 3
  • 36
  • 45
1

In my case my disk was full.

Here are some commands to verify this and find where the disk space is being taken. In my case it was the PHP log at over 20GB.

# see general disk space usage
df -h

# see current file and directory disk space usage. You can go to / and work your way in
du -sh *
kintsukuroi
  • 1,332
  • 16
  • 15
1

Backup location can be given in .vimrc, e.g.:

set backupdir=~/.vim/backup

You may need to create this directory yourself.

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
yhs
  • 11
  • 1
1

from within vi, try:

:w!

:help w! gives the following information:

                                                       *:w!*
:[range]w[rite]! [++opt] {file}
                        Write the specified lines to {file}.  Overwrite an
                        existing file.
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
0

Another possibility is that you put your backups in a centralized location (e.g. /tmp) and you edited a particular file as root. Then, the backup file will be owned by root and un-writeable by you as a mere mortal later.

In this case, the suggestion above to touch /tmp/atestfile won't show the problem! You'll have write permissions, no problem, but not to the particular backup file vim is trying to write.

Remove the file as root (sudo rm).

Note that the reason w! works is because vim writes the file without writing a backup file (you're insisting that it write despite the error).

Greg Bell
  • 2,032
  • 1
  • 18
  • 18
0

I just started using nvim and I found my issue was that my borrowed premade vimrc file had a preset source in it.

grep -rnw ~/.config/nvim/ -e backup
.config/nvim/lua/custom/vimrc:132:set backupdir=~/.backup/,/tmp/

Like others here, creating that backup directory cleared my error message (~/.backup/)

Slovpeis
  • 1
  • 2
0

I think it's also worth mentioning that the problem might not be the permissions you have in the backup directory. For me the file I was writing was in a root owned directory, although the file was user owned.

0

This massage might appear if you are editing a file that is not owned by you, i.e. you are editing a file in a directory owned by root.

you can use this command in the directory to see what is owned by you and what is owned by root.

~/test λ  ls -la .
total 8
drwxr-xr-x  2 root      root      4096 Jul 20 01:50 .
drwx------ 27 root      root      4096 Jul 20 01:49 ..
-rw-r--r--  1 user      user         0 Jul 20 01:50 hello

in this case hello file is owned by user but the directory . is owned by root. if you tried to edit the file and save with vim or neovim, it will display error Can't write to backup file (add ! to override).

so to solve this you can

sudo chown root:root hello
sudo vim hello

and it should work as expected.