tags:

views:

129

answers:

2
+1  Q: 

vim superuser mode

After opening a file in vim,And if that file is created by root .How to change the current user to superuser mode without quitting or before saving.

Thanks.

A: 

try this http://www.vim.org/scripts/script.php?script_id=729

shuvalov
but there is a way to do this from vim itself right?
Hulk
this script reads files as sudoer(sudo cat filename) and provide it to you for editing. You can do it yourself by executing :! sudo cat youfile > /tmp/file and then :e /tmp/file. So the script is a simple wrapper for this boring command.
shuvalov
+6  A: 

This should do the trick.

:w !sudo tee %

More tricks like that here: http://vim.wikia.com/wiki/Su-write

Ressu
Can you explain your answer?
Mosh
:w stands for writethe sudo tree command will spawn a tee process with superuser permissionsthe tee process redirect the standard input to both standard output and the file, which is given by %Also: nice trick.
Dacav
The only downside with that short version is that it will output the file to the screen while saving, you can remedy that by adding > /dev/null to the end of the command. But it's easier to write like that and i don't mind the extra output.
Ressu
Now it asks for the super user password and when i enter the password it says incorrect password.But the password entered is right..
Hulk
Sudo asks for the current user password. You also need to enable sudo usage with visudo if your distribution doesn't configure it properly for you.you can test your sudo setup with the command "sudo -l" which will list the commands you are allowed to run. If it fails or doesn't report any commands, you need to edit sudo configuration.
Ressu