Vim如何保存需要root权限的文件

来源:互联网 发布:健体与健美的区别 知乎 编辑:程序博客网 时间:2024/06/02 01:35

转自: http://feihu.me/blog/2014/vim-write-read-only-file/

在Linux上工作的朋友很可能遇到过这样一种情况,当你用Vim编辑完一个文件时,运行:wq保存退出,突然蹦出一个错误:

E45: 'readonly' option is set (add ! to override)

这表明文件是只读的,按照提示,加上!强制保存::w!,结果又一个错误出现:

"readonly-file-name" E212: Can't open file for writing

文件明明存在,为何提示无法打开?这错误又代表什么呢?查看文档:help E212

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 directoryor the file name is not valid.

原来是可能没有权限造成的。此时你才想起,这个文件需要root权限才能编辑,而当前登陆的只是普通用户,在编辑之前你忘了使用sudo来启动Vim,所以才保存失败。于是为了防止修改丢失,你只好先把它保存为另外一个临时文件temp-file-name,然后退出Vim,再运行sudo mv temp-file-name readonly-file-name覆盖原文件。

但这样操作过于繁琐。而且如果只是想暂存此文件,还需要接着修改,则希望保留Vim的工作状态,比如编辑历史,buffer状态等等,该怎么办?能不能在不退出Vim的情况下获得root权限来保存这个文件?

解决方案

答案是可以,执行这样一条命令即可:

:w !sudo tee %

0 0
原创粉丝点击