SUSEUnbound
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  PortalPortal  Latest imagesLatest images  SearchSearch  RegisterRegister  Log in  
openFate
Save a file you edited in vim without the needed permissions Fatelogo_small openFATE - openSUSE feature tracking
Similar topics
Latest topics
» Difference between 42.2 and 42.1
Save a file you edited in vim without the needed permissions Emptyby findoctr Thu Dec 15, 2016 7:53 pm

» openSUSE Leap 42.1 ?
Save a file you edited in vim without the needed permissions Emptyby findoctr Fri Feb 05, 2016 8:09 pm

» Happy Turkey Day
Save a file you edited in vim without the needed permissions Emptyby findoctr Thu Nov 26, 2015 1:45 pm

» Happy 4th of July!
Save a file you edited in vim without the needed permissions Emptyby bozo Sat Jul 04, 2015 12:56 pm

» It's been a while ...
Save a file you edited in vim without the needed permissions Emptyby bozo Mon Feb 23, 2015 8:34 pm

» Mondo chillers
Save a file you edited in vim without the needed permissions Emptyby bozo Wed Feb 18, 2015 5:11 am

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search
IRC Channel
You can also find us on IRC's freenode.net as #suseunbound.

 

 Save a file you edited in vim without the needed permissions

Go down 
AuthorMessage
welan
Admin
welan


Posts : 248
Join date : 2010-02-23
Age : 61
Location : snow drift in minnesota

Save a file you edited in vim without the needed permissions Empty
PostSubject: Save a file you edited in vim without the needed permissions   Save a file you edited in vim without the needed permissions EmptySat Jan 14, 2012 8:54 pm

:w !sudo tee %

This happens way too often. I open a system config file in vim and edit it just to find out that I don't have permissions to save it. This one-liner saves the day. Instead of writing the while to a temporary file :w /tmp/foobar and then moving the temporary file to the right destination mv /tmp/foobar /etc/service.conf, you now just type the one-liner above in vim and it will save the file.

Here is how it works, if you look at the vim documentation (by typing :he :w in vim), you'll find the reference to the command :w !{cmd} that says that vim runs {cmd} and passes it the contents of the file as standard input. In this one-liner the {cmd} part is the sudo tee % command. It runs tee % as superuser. But wait, what is %? Well, it's a read-only register in vim that contains the filename of the current file! Therefore the command that vim executes becomes tee current_filename, with the current directory being whatever the current_file is in. Now what does tee do? The tee command takes standard input and write it to a file! Rephrasing, it takes the contents of the file edited in vim, and writes it to the file (while being root)! All done!
Back to top Go down
 
Save a file you edited in vim without the needed permissions
Back to top 
Page 1 of 1
 Similar topics
-
» vlc/dvb-t Help Needed
» m2t file transcoding

Permissions in this forum:You cannot reply to topics in this forum
SUSEUnbound :: Help Section :: Tips and Tweaks-
Jump to: