0

Sometimes I open a file that needs admin permissions to be written but I forget to type sudo, so I open it with my regular user. (I usually use nano, but the question can be extended to other console editors, eg: vim)

Then I work on this file and when I try to save it I get the permission error for not being admin.

What I usually do then is either exit without saving, opening again with sudo and redoing the job.

I tried cutting all the file content and putting it to the clipboard hoping that I can exit the file, open it again with sudo and I can paste it. But when exiting nano it appears that whatever was cut and put in the clipboard is lost.

Forgetting to open a file with sudo is probably something that happens from time to time to everybody (at least to me). So I'm wondering if there's a way of saving the changes or saving the file as an admin even if nano was open by a regular user (typing the admin's password)

  • On Vim: [How does the vim “write with sudo” trick work?](https://stackoverflow.com/questions/2600783/how-does-the-vim-write-with-sudo-trick-work) – Matt Nov 28 '19 at 12:35
  • https://stackoverflow.com/search?q=%5Bsudo%5D+%5Bvim%5D+write – phd Nov 28 '19 at 12:59
  • I guess nano is too primitive to apply the same trick; there you'd have to "save as" to a (writable) temp location, exit, and then overwrite the original file with sudo. Another reason to switch to Vim :-) – Ingo Karkat Nov 28 '19 at 16:23

0 Answers0