265

I am trying to edit sources.list using vi editor but getting the following error while saving the file:

/etc/apt/sources.list" E212: Can't open file for writing
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
codersofthedark
  • 9,183
  • 8
  • 45
  • 70
  • 2
    I got this when the directory I was trying to write the file to exceeded its max. disc usage – Mehdi Nellen May 20 '15 at 11:31
  • 2
    As a comment mentioned in one of the answers, you might get this if folder in which the file resides has been deleted or didn't exist in the first place. Look for commands for changing directory or creating a directory from vi, depending on the situation. – kon psych Sep 08 '15 at 21:41
  • Check that the directory you are trying to write into exists. – Julian Espinel Aug 19 '22 at 16:31

19 Answers19

404

Vim has a builtin help system. Running :h E212 inside Vim prints the following:

For some reason the file you are writing to cannot be created or overwritten. The reason could be that you do not have permission to write in the directory or the file name is not valid.

You might want to edit the file as a superuser with sudo vim FILE. Or if you don't want to leave your existing vim session (and now have proper sudo rights), you can run:

:w !sudo tee % > /dev/null

Which will save the file.

Maëlan
  • 3,586
  • 1
  • 15
  • 35
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
  • 100
    Thanks! In my case it was trying to edit a non-existing file in a non-existing directory. Turns out, while you can "open" non-existing file and then create it by saving it, this doesn't work with non-existing directory. And now I know about the Vim's built-in help system! :) – Dom Delimar Nov 10 '12 at 16:38
  • Thanks! In my case, I was trying to write to a network drive, and my network permissions had partially crashed for that screen session. I ended up writing out to a temporary file in /var and copying that from another ssh instance over to my home directory. – Ross Aiken Apr 04 '13 at 19:54
  • Rolled back an [invalid edit](http://stackoverflow.com/review/suggested-edits/1999820#./1999820). – class stacker Apr 26 '13 at 10:40
  • @Xinus see this: http://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work – Zsolt Botykai May 07 '14 at 08:08
  • :h E212 says: ".bashrc" E212: Can't open file for writing E433: No tags file E149: Sorry, no help for E212 – hello_there_andy Jan 06 '15 at 17:08
  • @hello_there_andy most probably you have some issues with your Vim installation (e.g. not existing documentation and/or access right(s) problem(s)). – Zsolt Botykai Jan 06 '15 at 21:34
  • @ZsoltBotykai cheers and I think your diagnosis was near enough there... embarassingly... it fixed itself by restarting * facepalm * – hello_there_andy Jan 06 '15 at 22:16
37

That happens to me all the time, I open a root file for writing:

Instead of losing all your changes and re-opening with sudo. See this demo of how to save those changes:

One time Setup demo to create a root owned read only file for a lower user:

sudo touch temp.txt
sudo chown root:root temp.txt
sudo chmod 775 temp.txt
whoami
el

First open the file as normal user:

vi temp.txt

Then make some changes to the file, it warns you its read only. Use this command.

:w !chmod 777 %

Then write the file:

:wq!

The permissions are expanded, and the file is saved. You need the exclamation point because you are editing a root file as a lesser user.

Explanation of what that command does:

The :w means write the file. The bang means start interpreting as shell. chmod means change permissions, 777 means full permissions everywhere. The percent means the current file name.

It applies the change. And it ask if you want to re-load. Press "O" for "Ok". Don't reload or you'll lose your changes.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
24

For me there was was quite a simple solution. I was trying to edit/create a file in a folder that didn't exist. As I was already in the folder I was trying to edit/create a file in.

i.e. pwd folder/file

and was typing

sudo vim folder/file

and rather obviously it was looking for the folder in the folder and failing to save.

Nitisha
  • 101
  • 12
Simon
  • 241
  • 2
  • 2
11

I referenced to Zsolt in level 2, I input:

:w !sudo tee % > /dev/null

and then in my situation, I still can't modify the file, so it prompted that add "!". so I input

:q! 

then it works

oscarz
  • 1,184
  • 11
  • 19
10

Or perhaps you are on a readonly mounted fs

kellogs
  • 2,837
  • 3
  • 38
  • 51
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – DmitryG May 16 '13 at 11:10
  • 17
    @DmitryG There is no question being asked here, just a situation being stated. This was the reason for which I got into the very same situation and I think people would find it useful to know about it. – kellogs May 16 '13 at 16:41
7

because the dir is not exist.

can use :!mkdir -p /etc/apt/ to make the directory.

then :wq

Mr.Wang
  • 151
  • 2
  • 6
4

for me worked changing the filesystem from Read-Only before running vim:

bash-3.2# mount -o remount rw /
3

1. need to create directory, such as

mkdir -p /var/dir1/dir2

2. you can vi or vim you file, then modify, and save esc > .wq, such as

vim /var/dir1/dir2/filename.txt
Zgpeace
  • 3,927
  • 33
  • 31
3

Just open the file with -

sudo vi filename.txt

Then make the file changes and quit with :wq

2

change user to root

sodu su -

browse to etc

vi sudoers

look for root user in user priviledge section. you will get it like

root ALL=(ALL:ALL) ALL 

make same entry for your user name. if you username is 'myuser' then add

myuser ALL=(ALL:ALL) ALL

it will look like

root ALL=(ALL:ALL) ALL 

myuser ALL=(ALL:ALL) ALL 

save it. change root user to your user. now try the same where you were getting the sudoers issue

somspeaks
  • 534
  • 2
  • 6
  • 11
1

Try to connect as root and then edit file. This works for me

Kazman
  • 155
  • 2
  • 4
  • 12
1

Pre-append your commands with sudo.

For example, Instead of vim textfile.txt, used sudo vim textfile.txt. This will resolve the issue.

DannyDannyDanny
  • 838
  • 9
  • 26
1

Open the cmd.exe as "as administrator". That's all.

0

It might be possible that the file you are accessing has a swap copy (or swap version) already there in the same directory

Hence first see whether a hidden file exists or not.

For example, see for the following type of files

.system.conf.swp

By using the command

ls -a

And then, delete it using ...

rm .system.conf.swp

Usually, I recommend to start using super user privileges using ...

sudo su
0

I got this error when I used git rm on a file in a directory.

I was in something like ~/gitRepo/code/newFeature

In newFeature there was only one file. I did a git rm on that file then tried to create a new file myNewFile using vi.

Ubuntu showed me as still being inside the newFeature directory but actually git rm had removed the whole directory.

I had to exit out of vi, navigate up one directory and then recreate the newFeature directory.

DatabaseShouter
  • 814
  • 9
  • 11
0

I got this error when my directory path is incorrect, ensure your directory names and path are correct

0

change the permission for the other user for that type setfacl -m u:username:rw filename

0

:wq or :wq! the vi editor not exit means you have no permissons on the pwd, pls check the permissons then according to that you have made. example you have permissions with root

==> sudo vi /etc/ansible/hosts/ :wq now its work

-2

You just need to access to Gemfile with root access. Before vi:

command:

sudo su -

then:

vi ~/...
Wtower
  • 18,848
  • 11
  • 103
  • 80
Ali G
  • 29
  • 2