Cannot create backup file(add ! to overwrite) [closed]
Asked Answered
B

14

46

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 .

Barnardo answered 8/12, 2011 at 8:33 Comment(5)
@closevoter(s): Vi and Vim are tools used for programming and are therefore on topic.Cochleate
@IgnacioVazquez-Abrams: True enough, but keyboards are used by the majority of computer users. Modal text editors have a more specific user-base centred around programmers.Cochleate
@Johnsyweb: Feel free to convince the angry horde of sysadmins that will be beating on your door shortly...Dreyfus
I didn't suggest that SysAdmins don't use vi.Cochleate
Some programmers drive cars; that doesn't mean that a problem with unlocking a car is a programming issue.Unhandy
C
60

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.

Cochleate answered 8/12, 2011 at 8:38 Comment(8)
In my case the backups folder (~/.vim/backups) didn't exist. I created it, and the error went away.Baskerville
+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.Sazerac
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*Albumen
In my case the backupdir was ~/tmp and didn't exist. Creating it solved the issue.Trefler
This error occurs after you set the backup folder to peculiar one with set backupdir=$PATH, and $PATH does not exist.Neogaea
@Baskerville : in my case I added ~/.vim/backup (singular), backups (plural) did not work.Insistency
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.Extern
In my case, the backupext was set to backupext=~ and vim had exhausted all filenames starting with backupext. If you want to list these files, execute that command in vim : :echo join(['ls ',strcharpart(expand('%'),0,strcharlen(expand('%'))-1),'?','backupext],'')Expellant
U
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
Understructure answered 5/8, 2015 at 11:25 Comment(2)
In my case the $HOME directory (~) wasn't writeable anymore. Not sure how that happened, I don't remember chmodding anything.Outwork
Also make sure it exists, mine was deleted somehow somewhere.Melanymelaphyre
C
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.

Convexoconcave answered 7/9, 2015 at 7:0 Comment(0)
E
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

Evette answered 14/8, 2017 at 18:55 Comment(1)
Turns out I need a new 10000000000TB drive.Belike
G
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.

Ginnygino answered 26/2, 2018 at 19:18 Comment(0)
Z
3

I've fixed this with:

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

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

Zeuxis answered 27/8, 2018 at 15:57 Comment(0)
E
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.
Exuviae answered 8/12, 2011 at 8:39 Comment(0)
A
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 *
Alexandrite answered 19/12, 2019 at 23:0 Comment(0)
M
1

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

set backupdir=~/.vim/backup

You may need to create this directory yourself.

Maki answered 18/8, 2020 at 17:39 Comment(0)
B
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).

Bohs answered 30/8, 2016 at 6:7 Comment(0)
G
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/)

Gravid answered 10/8, 2022 at 6:44 Comment(0)
E
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.

Enlil answered 7/3, 2023 at 1:55 Comment(0)
L
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.

Leven answered 19/7, 2023 at 23:0 Comment(0)
C
0

Simple first free some space and try again..if still same issue then try to save with :w!

Cobos answered 4/10, 2023 at 15:12 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewGuv

© 2022 - 2024 — McMap. All rights reserved.