使用vim编辑文件时遇到没有权限的解决办法

Save a file you edited in vim without the needed permissions (no echo)

Save a file you edited in vim without the needed permissions (no echo)

Write a file you edited in Vim but that you do not have the permissions to write to (unless you use sudo.) Same as #1204 but without the echo to stdout that I find annoying.

不用sudo等来保存一个需要编辑权限的文件,保存一个你在写的文件文件,你发现你没有权限去写(除非你用sudo),此时使用下面的命令就可以解决
下面的命令解释,写文件到一个命令,tee读取stdin, 写到stdout和文件中,由于使用了sudo,所以可以写回文件,个人测试不用>/dev/null也可以直接就stdout到文件了
:w !sudo tee > /dev/null %

文章评论部分:
:w !sudo tee %

I often forget to sudo before editing a file I don't have write permissions on. When you come to save that file and get the infamous "E212: Can't open file for writing", just issue that vim command in order to save the file without the need to save it to a temp file and then copy it back again.


command W :execute ':silent w !sudo tee % > /dev/null' | :edit!

Calls sudo tee like all the other lines, but also automatically reloads the file.

Optionally you can add

command Wq :execute ':W' | :q

and

command WQ :Wq

to make quitting easier



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值