<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: WammKD</title>
    <description>The latest articles on DEV Community by WammKD (@wammkd_64).</description>
    <link>https://dev.to/wammkd_64</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F41265%2F2ac0e2c0-3af9-4da2-bbda-3719626e5305.png</url>
      <title>DEV Community: WammKD</title>
      <link>https://dev.to/wammkd_64</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wammkd_64"/>
    <language>en</language>
    <item>
      <title>The Emacs Trick Which Will Save Your Time and Nerves</title>
      <dc:creator>WammKD</dc:creator>
      <pubDate>Tue, 18 Sep 2018 04:44:20 +0000</pubDate>
      <link>https://dev.to/wammkd_64/the-emacs-trick-which-will-save-your-time-and-nerves-ln5</link>
      <guid>https://dev.to/wammkd_64/the-emacs-trick-which-will-save-your-time-and-nerves-ln5</guid>
      <description>&lt;p&gt;Alright; for those who read dev.to regularly, it may already be obvious that my title is derived from &lt;a href="https://dev.to/jovica/the-vim-trick-which-will-save-your-time-and-nerves-45pg"&gt;a particular other post&lt;/a&gt; (not to mention it being listed as one of the top 7 posts of the last week).&lt;/p&gt;

&lt;p&gt;But it reminded me of a problem I'd run into oh so often when using Emacs (probably for anyone who uses a text editor), one that I'd recently found a very satisfying solution to.&lt;/p&gt;

&lt;p&gt;For those who hadn't read the article, you may be doing some maintenance on some files; you'll probably notice immediately since you'll find that the file is read-only but, even if you give this no second thought and make it so you can edit the file, you'll be greeted, when you go to save, with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;File apg.conf is write-protected; try to save anyway? (yes or no)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And – if you think to yourself, "Yes! Of course I do!" and slam out a &lt;code&gt;yes&lt;/code&gt; – you'll be greeted with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Doing chmod: Operation not permitted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need &lt;code&gt;sudo&lt;/code&gt; but who wants to remember to do that every time?&lt;/p&gt;

&lt;p&gt;Now, there's been a widespread, instant solution for, at least, as long as I've looked for a solution – it involves the incredible &lt;a href="https://www.gnu.org/s/tramp/"&gt;Tramp&lt;/a&gt; package and instantly invokes &lt;code&gt;sudo&lt;/code&gt; when trying to save a file Emacs doesn't have permissions for.&lt;/p&gt;

&lt;p&gt;Recently, I found that, once saved, subsequent saves warn the user that the file has recently been edited (from your past save) and are you sure you want to save?&lt;/p&gt;

&lt;p&gt;Needless to say, this can become irritating quite quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter a Second Solution!
&lt;/h2&gt;

&lt;p&gt;Thankfully, there's a really simple solution and it gets invoked the second you turn off read-only mode instead of waiting all the way until you save: one single action indicating a desire to take responsibility for this file when you indicate you want to.&lt;/p&gt;

&lt;p&gt;First, define some advice for &lt;code&gt;read-only-mode&lt;/code&gt; (code gotten from &lt;a href="http://emacsredux.com/blog/2013/04/21/edit-files-as-root/"&gt;this blog&lt;/a&gt; with slight tweaks from me):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scheme"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defadvice&lt;/span&gt; &lt;span class="nv"&gt;read-only-mode&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;after&lt;/span&gt; &lt;span class="nv"&gt;read-only-mode-sudo&lt;/span&gt; &lt;span class="nv"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="s"&gt;"Find file as root if necessary."&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;buffer-file-name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;file-writable-p&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;buffer-file-name&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;p&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;point&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;find-alternate-file&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt; &lt;span class="s"&gt;"/sudo:root@localhost:"&lt;/span&gt; &lt;span class="nv"&gt;buffer-file-name&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;goto-char&lt;/span&gt; &lt;span class="nv"&gt;p&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simply tells &lt;code&gt;read-only-mode&lt;/code&gt; that, when activated, check that the buffer has a name and, if so, is the file not able to be written to.&lt;/p&gt;

&lt;p&gt;If so, grab the point of the cursor, open a new file (really, the same file but with &lt;code&gt;sudo&lt;/code&gt; via Tramp), and then go the point we saved since, reopening the file, the cursor would be at the beginning of the file, otherwise.&lt;/p&gt;

&lt;p&gt;Simple enough.&lt;/p&gt;

&lt;p&gt;However, I quickly found that I ran into a new problem – attempting to open a new file assumed that we should stay using &lt;code&gt;sudo&lt;/code&gt;: &lt;em&gt;definitely&lt;/em&gt; not an assumption I wanted made, by default, for other files.&lt;/p&gt;

&lt;p&gt;So I whipped up a function to replace the &lt;code&gt;find-file&lt;/code&gt; function (while using it to avoid departing from standard behavior too terribly):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scheme"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defun&lt;/span&gt; &lt;span class="nv"&gt;find-file-no-sudo&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;interactive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;find-file&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;read-file-name&lt;/span&gt;
               &lt;span class="s"&gt;"Find file: "&lt;/span&gt;
               &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;string-match&lt;/span&gt; &lt;span class="s"&gt;"^/sudo:root@localhost:"&lt;/span&gt; &lt;span class="nv"&gt;default-directory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;substring&lt;/span&gt; &lt;span class="nv"&gt;default-directory&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                 &lt;span class="nv"&gt;default-directory&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, – if there's the text necessary to use &lt;code&gt;sudo&lt;/code&gt; via Tramp at the beginning – strip it out!&lt;/p&gt;

&lt;p&gt;Now, to make sure we use this where we used the old function…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scheme"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;global-set-key&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;kbd&lt;/span&gt; &lt;span class="s"&gt;"C-x C-f"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ss"&gt;'find-file-no-sudo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it!&lt;/p&gt;

&lt;p&gt;Throw all of the above in your init file and you're good to go.&lt;/p&gt;

&lt;p&gt;Next time you find yourself with a file you wanted to edit but didn't open with &lt;code&gt;sudo&lt;/code&gt;, just do &lt;code&gt;C-x C-q&lt;/code&gt; and you'll prompted for your password; then edit to your heart's content.&lt;/p&gt;

</description>
      <category>emacs</category>
      <category>sudo</category>
    </item>
  </channel>
</rss>
