<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Solid State Raam &#187; VIM</title>
	<atom:link href="http://solidstateraam.com/category/vim/feed/" rel="self" type="application/rss+xml" />
	<link>http://solidstateraam.com</link>
	<description>Explorations (and exploitations) of the digital world by one of its many netizens.</description>
	<lastBuildDate>Thu, 13 May 2010 19:24:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Saving Files as root From Inside VIM</title>
		<link>http://solidstateraam.com/saving-files-as-root-from-inside-vim/</link>
		<comments>http://solidstateraam.com/saving-files-as-root-from-inside-vim/#comments</comments>
		<pubDate>Sat, 09 May 2009 02:53:36 +0000</pubDate>
		<dc:creator>Raam</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[VIM]]></category>

		<guid isPermaLink="false">http://solidstateraam.com/?p=187</guid>
		<description><![CDATA[Oftentimes I will be editing a Linux configuration file using vim only to discover that I cannot save it because the file requires root permission to write to it. See how I solved this problem using the linux <code>tee</code> utility.]]></description>
			<content:encoded><![CDATA[<p>Oftentimes I will be editing a Linux configuration file using vim only to discover that I cannot save it because the file requires root permission to write to it. This ends up looking something like this:</p>
<pre class="brush: bash;">
vi /path/to/some/file.conf
[make some edits]
:w
VIM Message: E45: 'readonly' option is set (add ! to override)
:q!
$ sudo vi /path/to/some/file.conf
[make all my edits AGAIN]
:w
</pre>
<p>I have gone through this process <em>so many times</em> that I knew there must be an easy fix for it. (I know about <code>sudo !!</code> for running the previous command, but I only recently started developing the habit of using it.) After forgetting to use sudo while editing a configuration file yet again this morning, I finally decided to search Google and find a solution. Here it is:</p>
<pre class="brush: bash;">
vi /path/to/some/file.conf
[make some edits]
:w
VIM Message: E45: 'readonly' option is set (add ! to override)
:w !sudo tee %
</pre>
<p>The <code>:w !sudo tee %</code> command tells VIM to write the file (<code>w</code>) but run the sudo command first (<code>!sudo</code>) and read the writing of the file from standard input to standard output (<code>tee</code>) using the same filename as the one we&#8217;re editing (<code>%</code>).</p>
<p>After saving the file as root, you&#8217;ll get this message: &#8220;W12: Warning: File &#8220;/private/etc/smb.conf&#8221; has changed and the buffer was changed in Vim as well&#8221;. You&#8217;ll be given the option to reload it, but since you were already looking at the new version it doesn&#8217;t much matter which option you choose (OK or Reload).</p>
<p>And last but not least, if you don&#8217;t want to remember the syntax for this command, you can map the command in your ~/.vimrc file:</p>
<pre class="brush: bash;">
cmap w!! w !sudo tee % &gt;/dev/null
</pre>
<p>Now, if you forget to edit a file with sudo, you can simply type <code>:w!!</code> to fix the problem!</p>
]]></content:encoded>
			<wfw:commentRss>http://solidstateraam.com/saving-files-as-root-from-inside-vim/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
