PDA

View Full Version : becoming root inside vim



purpleidea
May 4th, 2008, 11:39 PM
Hey,
too often when browsing as normal user in say /etc i:

$ vim <filename>

to start editing and then when i go to :wq to save i realize i had forgotten to start it all off with:

$ sudo vim <filename>

and i now don't have permission to save. I know that i can :w /tmp/x and then:

$ sudo cp /tmp/x /etc/<filename>

but i was wondering if someone perhaps knew of some nice vim trick i could do so that i can save without retyping all the text..., sort of the way you can "become root" from within aptitude

many thanks!
_J

LaRoza
May 4th, 2008, 11:49 PM
but i was wondering if someone perhaps knew of some nice vim trick i could do so that i can save without retyping all the text..., sort of the way you can "become root" from within aptitude


Yes, run the editor with sudo when you do it.

dwhitney67
May 5th, 2008, 12:12 AM
I'm not aware of any trick, other than perhaps doing something (stupid) like changing the permissions of the file so that it is writable by a regular user, and then after the write is complete, reverting back to the original permissions.

While in vim, you can issue shell commands using the :!<cmd>, where <cmd> could be something like "sudo chmod 666 filename". When you are done, repeat the command but change the permissions to 644.

Personally I think this is crude and should be avoided. When you use vim to open a protected file, it tells you at the very bottom of the editor that it is a read-only file. Try to get into the habit at looking at the bottom of the editor when you first edit a file.

samjh
May 5th, 2008, 12:22 AM
but i was wondering if someone perhaps knew of some nice vim trick i could do so that i can save without retyping all the text..., sort of the way you can "become root" from within aptitudeI don't know of any tricks like that within vim.

You could always :w ~/temp.file first and then sudo cp ~/temp.file /destination/dir later.

stroyan
May 5th, 2008, 12:34 AM
That is an interesting question.
I sometimes find myself writing temporary files and thinking "I'll be baack".
There is a sudo vi feature at
http://www.vim.org/scripts/script.php?script_id=729
that has a special sudo prefix for opening a file with sudo.
Then you could actually use split buffers to merge in changes.

Taking a tactic from that script it would be possible to use a command map
to do an sudo write from a : command.
Warm up with putting this in .vimrc-

cmap w!! %!sudo tee > /dev/null %
then modify a protected file and try to write with
:w
then
:w!
then
:w!!

Got it!

mssever
May 5th, 2008, 07:36 AM
I make a wrapper for vim. I call this script ~/bin/vi (I'm used to typing vi instead of vim, anyway):
#!/bin/bash

precommand=
for i in "$@" ; do
if [ -f "$i" -a ! -w "$i" ] ; then precommand="sudo " ; break ; fi
done

exec ${precommand}vim "$@"This script runs vim with sudo if any file mentioned on the command line isn't writable.

For the way I work, this isn't risky. Everything (with a few minor exceptions) outside of $HOME isn't user-writable. So the only safety against accidentally doing something stupid is knowing what I'm editing--no matter whether I typed sudo or not.

purpleidea
July 10th, 2008, 10:18 AM
That is an interesting question.
I sometimes find myself writing temporary files and thinking "I'll be baack".
There is a sudo vi feature at
http://www.vim.org/scripts/script.php?script_id=729
that has a special sudo prefix for opening a file with sudo.
Then you could actually use split buffers to merge in changes.

Taking a tactic from that script it would be possible to use a command map
to do an sudo write from a : command.
Warm up with putting this in .vimrc-

cmap w!! %!sudo tee > /dev/null %
then modify a protected file and try to write with
:w
then
:w!
then
:w!!

Got it!

took me a bit of effort to figure this out, but ultimately this was the perfect answer / solution.
many thanks !

_J

542
September 2nd, 2008, 08:52 AM
took me a bit of effort to figure this out, but ultimately this was the perfect answer / solution.
many thanks !

_J

This is exactly what I'd also like to do - I've placed the line in my .vimrc and tried typing w!! which seems to do nothing. What am I missing?

MONODA
September 2nd, 2008, 09:22 AM
whenever that happens to me I just do :q! which should force it to save.

samjh
September 2nd, 2008, 11:04 AM
whenever that happens to me I just do :q! which should force it to save.
Actually that forces vim to quit without saving. ;)

lunchlady55
December 22nd, 2008, 05:46 AM
This is what you're looking for.

:w !sudo tee %

It will then print "press [ENTER] to continue" and warn you the file's changed.

singlow
March 24th, 2009, 08:31 AM
maybe you are looking for:

:w !sudo tee %

you can save the file after authenticating, but you will return to the buffer as the original user and it will inform you that it was changed by another user and you can reload the buffer to continue.

edit: (or maybe I should have looked at results page 2 before figuring this out myself)

monkeyking
March 24th, 2009, 04:20 PM
actually that forces vim to quit without saving. ;)

lol