{"id":133,"date":"2016-05-01T21:43:42","date_gmt":"2016-05-01T13:43:42","guid":{"rendered":"https:\/\/wangqf.com\/?p=133"},"modified":"2016-05-01T21:43:42","modified_gmt":"2016-05-01T13:43:42","slug":"saving-a-read-only-file-edited-in-vi-vim","status":"publish","type":"post","link":"https:\/\/wangqf.com\/?p=133","title":{"rendered":"Saving a read-only file edited in vi \/ vim"},"content":{"rendered":"<p>We\u2019ve all done it\u2026opened a file using vi or vim to inspect the contents, and realize we need to alter it.\u00a0 Of course we totally ignored the message informing we didn\u2019t have permission to edit, so we\u2019re only allowed to view it as \u201cread-only\u201d.\u00a0 Then after we find the troublesome spot we hit \u201ci\u201d and happily edit the place needing changed, only to \u201cface-palm\u201d when we realize we cannot save the wonderful edit we just made.<\/p>\n<p>In the past I handled this one of three ways: I either copied and pasted the change after reopening the file using sudo, or I reopened and retyped everything once again, or I save the file as a temp file and then rename it using sudo.\u00a0 Very stupid, stressful and time consuming.<\/p>\n<p>However, now I know a better way.\u00a0 Using a combination of \u2018tee\u2019 and \u2018sudo\u2019 commands I can now save the read-only file rather than jumping through the hoops in the previous paragraph.\u00a0 Here is how:<\/p>\n<p>Open a file as normal, forgetting to use \u201csudo\u201d, and therefore viewing a read-only file.<br \/>\n<a href=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/001-b_open_read_only.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-630\" src=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/001-b_open_read_only.png\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" srcset=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/001-b_open_read_only-300x175.png 300w, http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/001-b_open_read_only.png 562w\" alt=\"001-b_open_read_only\" width=\"506\" height=\"295\" \/><\/a><\/p>\n<p>Then mistakenly try to edit the read-only file in the traditional manner.<br \/>\n<a href=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/002-b_insert-in-readonly-file.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-631\" src=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/002-b_insert-in-readonly-file.png\" sizes=\"auto, (max-width: 505px) 100vw, 505px\" srcset=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/002-b_insert-in-readonly-file-300x39.png 300w, http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/002-b_insert-in-readonly-file.png 505w\" alt=\"002-b_insert-in-readonly-file\" width=\"505\" height=\"66\" \/><\/a><\/p>\n<p>But when we try to save using \u2018:w!\u2019, SHIFT+ZZ, or :qw!, or whatever combination we normally fail with as an alternative.\u00a0 Here is sample output of what we see:<br \/>\n<a href=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/003-b_try-to-save.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-632\" src=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/003-b_try-to-save.png\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" srcset=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/003-b_try-to-save-300x46.png 300w, http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/003-b_try-to-save.png 671w\" alt=\"003-b_try-to-save\" width=\"483\" height=\"74\" \/><\/a><\/p>\n<p>At this point is where the new magic can happen. Instead of the normal \u201cface-palm\u201d we <strong>do not \u201cENTER\u201d<\/strong> and move on. We can enter a new command and successfully save the file after entering the sudo password:<br \/>\n<a href=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/005-b_sudo-password.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-634\" src=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/005-b_sudo-password.png\" alt=\"005-b_sudo-password\" width=\"463\" height=\"88\" \/><\/a><\/p>\n<p>At this point we will be presented with the content of the file and a prompt to press ENTER or type another command. To simply save the file and move on we just press ENTER, and then press the letter \u201cO\u201d (oh). (NOTE: \u201cL\u201d seems to do pretty much the same thing.)\u00a0 The file will be saved but remains open in vi\/vim for more editing or reading.\u00a0 We can now exit normally by typing \u201c:q!\u201d since the file is still open as \u201cread-only\u201d.<br \/>\n<a href=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/006-b_final-save.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-635\" src=\"http:\/\/www.geekyboy.com\/wp-content\/uploads\/2013\/04\/006-b_final-save.png\" alt=\"006-b_final-save\" width=\"470\" height=\"106\" \/><\/a><\/p>\n<p>What the command does:<\/p>\n<ul>\n<li>:w = Write a file.<\/li>\n<li>!sudo = Call shell sudo command.<\/li>\n<li>tee = The output of the vi\/vim write command is redirected using tee.<\/li>\n<li>% = Triggers the use of the current filename.<\/li>\n<li>Simply put, the \u2018tee\u2019 command is run as sudo and follows the vi\/vim command on the current filename given.<\/li>\n<\/ul>\n<p>[\u8f6c\u8f7d]http:\/\/www.geekyboy.com\/archives\/629<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We\u2019ve all done it\u2026opened a file using vi or vim to insp &hellip; <a href=\"https:\/\/wangqf.com\/?p=133\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cSaving a read-only file edited in vi \/ vim\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"https:\/\/wangqf.com\/index.php?rest_route=\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wangqf.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wangqf.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wangqf.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wangqf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=133"}],"version-history":[{"count":0,"href":"https:\/\/wangqf.com\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions"}],"wp:attachment":[{"href":"https:\/\/wangqf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wangqf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wangqf.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}