在vi中获取文件的root权限? [英] Getting root permissions on a file inside of vi?

查看:205
本文介绍了在vi中获取文件的root权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在编辑配置文件时,我会用vi打开一个文件,然后在保存时意识到我没有打字

Often while editing config files, I'll open one with vi and then when I go to save it realize that I didn't type

sudo vi filename

有什么办法赋予vi sudo特权来保存文件?我似乎回想起不久前在查找有关vi的一些东西时看到了一些有关此的信息,但现在找不到了.

Is there any way to give vi sudo privileges to save the file? I seem to recall seeing something about this while looking up some stuff about vi a while ago, but now I can't find it.

推荐答案

%替换为当前文件名,因此您可以使用:

% is replaced with the current file name, thus you can use:

:w !sudo tee %

(vim将检测到文件已更改,并询问是否要重新加载该文件.请选择[L]而不是确定".)

(vim will detect that the file has been changed and ask whether you want to it to be reloaded. Say yes by choosing [L] rather than OK.)

作为快捷方式,您可以定义自己的命令.将以下内容放入您的.vimrc:

As a shortcut, you can define your own command. Put the following in your .vimrc:

command W w !sudo tee % >/dev/null

使用上面的命令,您可以键入:W<Enter>保存文件.自从我写这篇文章以来,我发现了一种更好的方法(我认为):

With the above you can type :W<Enter> to save the file. Since I wrote this, I have found a nicer way (in my opinion) to do this:

cmap w!! w !sudo tee >/dev/null %

这样,您可以键入:w!!,它将扩展到完整的命令行,将光标留在末尾,因此您可以根据需要用自己的文件名替换%.

This way you can type :w!! and it will be expanded to the full command line, leaving the cursor at the end, so you can replace the % with a file name of your own, if you like.

这篇关于在vi中获取文件的root权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆