问题描述
今天在根目录下,使用vim编辑器编写了一段程序,但是在保存的时候被提示:E505:“file” is read-only(add ! to override )
强制写入呢?结果是E212: Can't open file for writing.
我们使用vim自带的帮助文档,可以看到
Cannot open "{filename}" for writing
Can't open file for writing
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.
这是由于没有root权限造成的。
解决方法一(tee)
这时我们需要输入这样一段命令
:w !sudo tee %
解释一下这行命令什么意思?
w的意思
通过在vim下查看w的帮助文档:
*:w_c* *:write_c*
:[range]w[rite] [++opt] !{cmd}
Execute {cmd} with [range] lines as standard input
(note the space in front of the '!'). {cmd} is
executed like with ":!{cmd}", any '!' is replaced with
the previous command |:!|.
The default [range] for the ":w" command is the whole buffer (1,$). If you
write the whole buffer, it is no longer considered changed. When you
write it to a different file with ":w somefile" it depends on the "+" flag in
'cpoptions'. When included, the write command will reset the 'modified' flag,
even though the buffer itself may still be different from its file.
接下来是一个叹号!,它表示其后面部分是外部命令,即sudo t