{"id":2340,"date":"2017-06-08T13:21:40","date_gmt":"2017-06-08T13:21:40","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=2340"},"modified":"2024-08-28T12:18:56","modified_gmt":"2024-08-28T12:18:56","slug":"save-file-vim-forgetting-use-sudo","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/save-file-vim-forgetting-use-sudo\/","title":{"rendered":"How to Save a File in Vim After Forgetting to Use Sudo"},"content":{"rendered":"<p>Many of you probably experienced this. You edit a file in Linux with Vim or Nano or whatever else text editor you might be using\u00a0and when you try to save, you realise you forgot to launch the editor with sudo privileges, and file can&#8217;t be written. Exiting without saving and editing again with sudo privileges is not always an option, especially if you lose a lot of work.<\/p>\n<h2>Solutions to save a file in vim<\/h2>\n<p>There are some workarounds. You can open a new terminal and grant the right permissions on the file with:<\/p>\n<pre class=\"toolbar:2 striped:false marking:false ranges:false nums:false nums-toggle:false lang:default decode:true\">sudo chmod 666 &lt;filename&gt;<\/pre>\n<p>Now you can go back to your editor and saving will be possible. Don&#8217;t forget to change back the right permissions for your file.<\/p>\n<p>Also, you could save in a new file and then copy the newly created file over the old one.<\/p>\n<p>But these are workarounds. Vim editor actually offers a solution.<\/p>\n<p>In normal(command line) mode of Vim you can issue:<\/p>\n<pre class=\"toolbar:2 striped:false marking:false ranges:false nums:false nums-toggle:false lang:vim decode:true \">:w !sudo tee %<\/pre>\n<p>And that works like magic. For the geeks, here is how the &#8220;magic&#8221; works:<\/p>\n<ul>\n<li>:w &#8211; writes the contents of the buffer<\/li>\n<li>!sudo &#8211; pipes it to the stdin of sudo<\/li>\n<li>tee % &#8211; sudo executes tee that writes to &#8220;%&#8221; file<\/li>\n<li>% &#8211; Vim expands &#8220;%&#8221; to current file name<\/li>\n<\/ul>\n<p>So Vim actually makes magic happen.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many of you probably experienced this. You edit a file in Linux with Vim or Nano or whatever else text [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":2350,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,77],"tags":[109,125,163,255],"yst_prominent_words":[480,1931],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2340"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=2340"}],"version-history":[{"count":4,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2340\/revisions"}],"predecessor-version":[{"id":133349,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2340\/revisions\/133349"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/2350"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=2340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=2340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=2340"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=2340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}